This project is using weather api for featch data and display based on user input data
Weather Cubit
classWeatherCubitextendsCubit<WeatherState> {
//_weatherReposotory object for fetch data from ReposotoryfinalWeatherReposotory _weatherReposotory;
WeatherCubit(
this._weatherReposotory,
) :super(constWeatherInitial());
//getWeather function use for get weather data from Reposotory.Future<void>getWeather(String cityName) async {
//notify frontend to Weather loading stateemit(constWeatherLoading());
try {
final weather =await _weatherReposotory.getWeatherLocationData(cityName);
//notify frontend to Weather loaded state with weather dataemit(WeatherLoaded(weather, cityName));
} onSocketException {
//notify frontend to WeatherError state with SocketExceptionemit(constWeatherError("400"));
} catch (e) {
final failure = e asFailure;
//notify frontend to WeatherError state with failure messageemit(WeatherError(failure.message));
}
}
}
Weather Repository
classWeatherReposotory {
//api object use for fetch data from apifinalWeatherApi api;
WeatherReposotory({requiredthis.api});
//getWeatherLocationData function is asyncrones method and using fetch data and return data to cubitFuture<dynamic>getWeatherLocationData(String cityName) async {
//rawWeather get response from api using user enter city namefinalResponse rawWeather =await api.getWeatherRawData(cityName);
if (rawWeather.statusCode ==200) {
//decode jason response body and map body dataMap<String, dynamic> weatherMap =jsonDecode(rawWeather.body);
//Map cityname and temp using weather model and return weather datavar weather =Weather.fromJson(weatherMap);
return weather;
} else {
throwconstFailure(message:'404');
}
}
}
Weather Api
classWeatherApi {
//base url. url end pointstaticconstString _baseUrl ="http://api.openweathermap.org/data/2.5/weather?q=";
//Api keystaticconstString _apiKey ="01cc8328d04c516c03c84af29cd9c0d9";
final http.Client _client;
WeatherApi({http.Client? client}) : _client = client ?? http.Client();
voiddispose() {
_client.close();
}
//Fectch weather data from apiFuture<dynamic>getWeatherRawData(String cityName) async {
final url ='$_baseUrl$cityName&appid=$_apiKey';
final response =await _client.post(
Uri.parse(url),
);
return response;
}
}
```# flutter_bloc_cubit_test_knowdge
This project follows the Reso Coder course for flutter test-driven-development with clean architecture and BloC state management for a random trivia simple app.
Cubit is a lightweight state management solution. It is a subset of the bloc package that does not rely on events and instead uses methods to emit new states.
Building a simple Flutter app * Switch Theme * for understanding the BLoC State Management including: Cubit Communications with StreamSubscription & Managing Route.
Egymation ?? ยท This application was developed using a well-defined and decoupled architecture, following TDD (test-driven programming) as a working me