Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate new null safety version #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/bloc/weather_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_cubit_bloc_tutorial/data/model/weather.dart';
import 'package:flutter_cubit_bloc_tutorial/data/weather_repository.dart';
import 'package:meta/meta.dart';
Expand Down
5 changes: 4 additions & 1 deletion lib/bloc/weather_event.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
part of 'weather_bloc.dart';

@immutable
abstract class WeatherEvent {}
abstract class WeatherEvent extends Equatable {}

class GetWeather extends WeatherEvent {
final String cityName;

GetWeather(this.cityName);

@override
List<Object?> get props => [cityName];
}
26 changes: 9 additions & 17 deletions lib/bloc/weather_state.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
part of 'weather_bloc.dart';

@immutable
abstract class WeatherState {
abstract class WeatherState extends Equatable {
const WeatherState();
}

class WeatherInitial extends WeatherState {
const WeatherInitial();

@override
List<Object?> get props => [];
}

class WeatherLoading extends WeatherState {
const WeatherLoading();

@override
List<Object?> get props => [];
}

class WeatherLoaded extends WeatherState {
final Weather weather;
const WeatherLoaded(this.weather);

@override
bool operator ==(Object o) {
if (identical(this, o)) return true;

return o is WeatherLoaded && o.weather == weather;
}

@override
int get hashCode => weather.hashCode;
List<Object?> get props => [weather];
}

class WeatherError extends WeatherState {
final String message;
const WeatherError(this.message);

@override
bool operator ==(Object o) {
if (identical(this, o)) return true;

return o is WeatherError && o.message == message;
}

@override
int get hashCode => message.hashCode;
List<Object?> get props => [message];
}
1 change: 1 addition & 0 deletions lib/cubit/weather_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_cubit_bloc_tutorial/data/model/weather.dart';
import 'package:flutter_cubit_bloc_tutorial/data/weather_repository.dart';
import 'package:meta/meta.dart';
Expand Down
26 changes: 9 additions & 17 deletions lib/cubit/weather_state.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
part of 'weather_cubit.dart';

@immutable
abstract class WeatherState {
abstract class WeatherState extends Equatable {
const WeatherState();
}

class WeatherInitial extends WeatherState {
const WeatherInitial();

@override
List<Object?> get props => [];
}

class WeatherLoading extends WeatherState {
const WeatherLoading();

@override
List<Object?> get props => [];
}

class WeatherLoaded extends WeatherState {
final Weather weather;
const WeatherLoaded(this.weather);

@override
bool operator ==(Object o) {
if (identical(this, o)) return true;

return o is WeatherLoaded && o.weather == weather;
}

@override
int get hashCode => weather.hashCode;
List<Object?> get props => [weather];
}

class WeatherError extends WeatherState {
final String message;
const WeatherError(this.message);

@override
bool operator ==(Object o) {
if (identical(this, o)) return true;

return o is WeatherError && o.message == message;
}

@override
int get hashCode => message.hashCode;
List<Object?> get props => [message];
}
19 changes: 5 additions & 14 deletions lib/data/model/weather.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import 'package:meta/meta.dart';
import 'package:equatable/equatable.dart';

class Weather {
class Weather extends Equatable {
final String cityName;
final double temperatureCelsius;

Weather({
@required this.cityName,
@required this.temperatureCelsius,
required this.cityName,
required this.temperatureCelsius,
});

@override
bool operator ==(Object o) {
if (identical(this, o)) return true;

return o is Weather &&
o.cityName == cityName &&
o.temperatureCelsius == temperatureCelsius;
}

@override
int get hashCode => cityName.hashCode ^ temperatureCelsius.hashCode;
List<Object?> get props => [cityName, temperatureCelsius];
}
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_cubit_bloc_tutorial/bloc/weather_bloc.dart';
import 'package:flutter_cubit_bloc_tutorial/cubit/weather_cubit.dart';
import 'package:flutter_cubit_bloc_tutorial/data/weather_repository.dart';
import 'package:flutter_cubit_bloc_tutorial/pages/weather_search_page.dart';

Expand Down
4 changes: 2 additions & 2 deletions lib/pages/weather_search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _WeatherSearchPageState extends State<WeatherSearchPage> {
child: BlocConsumer<WeatherBloc, WeatherState>(
listener: (context, state) {
if (state is WeatherError) {
Scaffold.of(context).showSnackBar(
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.message),
),
Expand Down Expand Up @@ -97,7 +97,7 @@ class CityInputField extends StatelessWidget {
}

void submitCityName(BuildContext context, String cityName) {
final weatherBloc = context.bloc<WeatherBloc>();
final weatherBloc = context.read<WeatherBloc>();
weatherBloc.add(GetWeather(cityName));
}
}
Loading