Flutter bloc - Show snackbar on state change - flutter

I am trying to log in with Google. When googleSignInAccount I yield new state with PlatformExecption. Now, How can I access that value in order to do some changes.
try{
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
if(googleSignInAccount == null){
yield GoogleLoginErrorState(error: PlatformException(code: 'user-cancel'));
}
}
my state.dart
class GoogleLoginErrorState extends GoogleLoginState {
final PlatformException error;
GoogleLoginErrorState({this.error});
#override
List<Object> get props => [error];
}
my BlocBuilder
if (state == GoogleLoginErrorState()) {
}

For side effects such as showing snackbars / dialogs or navigating to another screen, you have to use BlocListener, something like this:
BlocListener<YourGoogleSigninBloc, YourGoogleSigninState>(
listener: (context, state) {
if(state is GoogleLoginErrorState){
// show snackbar here
}
},
child: YourWidget(),
)
you can also use BlocConsumer instead of nesting BlocListeners and BlocBuilders like this:
BlocConsumer<YourGoogleSigninBloc, YourGoogleSigninState>(
listener: (context, state) {
if(state is GoogleLoginErrorState){
// show snackbar here
}
},
builder: (context, state) {
return YourWidget();
},
)

In the bloc builder,
if (state is GoogleLoginErrorState) {
}
This checks if the state data type is GoogleLoginErrorState
And use the state
Text(state.error.code)

Related

Flutter set state not updating my UI with new data

I have a ListView.builder widget wrapped inside a RefreshIndicator and then a FutureBuilder. Refreshing does not update my list, I have to close the app and open it again but the refresh code does the same as my FutureBuilder.
Please see my code below, when I read it I expect the widget tree to definitely update.
#override
void initState() {
super.initState();
taskListFuture= TaskService().getTasks();
}
#override
Widget build(BuildContext context) {
return Consumer<TaskData>(builder: (context, taskData, child) {
return FutureBuilder(
future: taskListFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
taskData.tasks = (snapshot.data as ApiResponseModel).responseBody;
return RefreshIndicator(
onRefresh: () async {
var responseModel = await TaskService().getTasks();
setState(() {
taskData.tasks = responseModel.responseBody;
});
},
child: ListView.builder(
...
...
Let me know if more code is required, thanks in advance!
Points
I am using a StatefulWidget
Task data is a class that extends ChangeNotifier
When I debug the refresh I can see the new data in the list, but the UI does not update
getTasks()
Future<ApiResponseModel> getTasks() async {
try {
var _sharedPreferences = await SharedPreferences.getInstance();
var userId = _sharedPreferences.getString(PreferencesModel.userId);
var response = await http.get(
Uri.parse("$apiBaseUrl/$_controllerRoute?userId=$userId"),
headers: await authorizeHttpRequest(),
);
var jsonTaskDtos = jsonDecode(response.body);
var taskDtos= List<TaskDto>.from(
jsonTaskDtos.map((jsonTaskDto) => TaskDto.fromJson(jsonTaskDto)));
return ApiResponseModel(
responseBody: taskDtos,
isSuccessStatusCode: isSuccessStatusCode(response.statusCode));
} catch (e) {
return null;
}
}
The issue here seems to be that you are updating a property that is not part of your StatefulWidget state.
setState(() {
taskData.tasks = responseModel.responseBody;
});
That sets a property part of TaskData.
My suggestion is to only use the Consumer and refactor TaskService so it controls a list of TaskData or similar. Something like:
Provider
class TaskService extends ChangeNotifier {
List<TaskData> _data;
load() async {
this.data = await _fetchData();
}
List<TaskData> get data => _data;
set data(List<TaskData> data) {
_data = data;
notifyListeners();
}
}
Widget
class MyTaskList extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Consumer<TaskService>(builder: (context, service, child) {
return RefreshIndicator(
onRefresh: () {
service.getTasks();
},
child: ListView.builder(
itemCount: service.data.length,
itemBuilder: (BuildContext context, int index) {
return MyTaskItem(data:service.data[index]);
},
),
);
});
}
}
and make sure to call notifyListeners() in the service.getTasks() method to make the Consumer rebuild
I think (someone will correct me if I'm wrong) the problem is that you are using the FutureBuilder, once it's built, you need to refresh to whole widget for the FutureBuilder to listen to changes. I can suggest a StreamBuilder that listens to any changes provided from the data model/api/any kind of stream of data. Or better yet, you can use some sort of state management like Provider and use Consumer from the Provider package that notifies the widget of any changes that may occurred.

Stream builds a stack of Widget

So, I am using a stream to track the user's authentication state. Here is my setup, which works fine so far.
class Root extends ConsumerWidget {
final Widget _loadingView = Container(color: Colors.white, alignment: Alignment.center, child: UiHelper.circularProgress);
#override
Widget build(BuildContext context, ScopedReader watch) {
return watch(userStreamProvider).when(
loading: () => _loadingView,
error: (error, stackTrace) => _loadingView,
data: (user) => user?.emailVerified == true ? Products() : Login(),
);
}
}
The problem is, stream builds the UI multiple times. And I have a welcome dialog inside of my products page, which opens multiple times and as soon as I start the app it becomes a mess.
What should I do to avoid this scenario?
** Here I am using riverpod package
I personally recommend wrapping your widget with a StreamBuilder using the onAuthStateChanged stream. This stream automatically updates when the user change its state (logged in or out). Here is an example that may help you!
Stream<FirebaseUser> authStateChanges() {
FirebaseAuth _firebaseInstance = FirebaseAuth.instance;
return _firebaseInstance.onAuthStateChanged;
}
return StreamBuilder(
stream: authStateChanges(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
// isLoggedIn
} else if (snapshot.hasData == false &&
snapshot.connectionState == ConnectionState.active) {
// isLoggedOut
} else {
// loadingView
}
},
);

Flutter Bloc vs Provider State Manament for "InProgress"

I can manage InProgress state via "yield" operator in Flutter Bloc,
My bloc:
#override
Stream<ContentState> mapEventToState(
ContentEvent event,
) async* {
if (event is ContentStarted) {
yield ContentLoadInProgress(); //yeah
var content= await repository.getContent();
yield ContentLoadSuccess(content);
}
...
}
page:
builder: (context, state) {
if (state is ContentInProgress) {
return LoadingWidget(); //showing CircularProgressIndicator Widget
} else if (state is ContentLoadSuccess) {
return Text(state.content);
}
(States : InitState,ContentLoadInProgress, ContentLoadSuccess, ContentLoadFailure)
How can I manage "ContentLoadInProgress" state in Provider State Management?
You can keep your states as enum
enum ContentStates {
InitState,
ContentLoadInProgress,
ContentLoadSuccess,
ContentLoadFailure,
}
In your provider class:
class ContentProvider with ChangeNotifier {
ContentState state = ContentStates.InitState;
Content content;
yourEvent() {
state = ContentStates.ContentLoadInProgress;
notifyListeners(); // This will notify your listeners to update ui
yourOperations();
updateYourContent();
state = ContentStates.ContentLoadSuccess;
notifyListeners();
}
}
Inside your widget you can use Consumer (Assuming you already used ChangeNotifierProvider above in your widget tree)
Consumer(
builder: (context, ContentProvider provider, _) {
if (provider.state == ContentStates.ContentLoadInProgress) {
return LoadingWidget();
} else if (provider.state == ContentStates.ContentLoadSucces) {
// use provider.content to get your content
return correspondingWidget();
} else if .... // widgets for other states
}
)

Delay state check inside Flutter bloc listener

I'm sending data to the server via a bloc and showing a progressSnackBar during, and then a successSnackBar on success. Sometimes this takes less than a second and it makes sense to not show the progressSnackBar at all - in other words wait a second and then check if the state is still UpdatingAccount. I've tried and failed with different combinations involving Future.delay(...) and I can probably do a setState hack but is there a way to achieve this just inside the bloc listener?
BlocListener<AccountBloc, AccountState>(
listener: (BuildContext context, state) {
if (state is UpdatingAccount) { // <-- delay this
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(progressSnackBar());
} else if (state is AccountUpdated) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(successSnackBar());
}
},
// rest...
),
I ended up making the widget stateful and giving it an _updated bool member.
BlocListener<AccountBloc, AccountState>(
listener: (BuildContext context, state) {
if (state is UpdatingAccount) {
_updated = false;
Future.delayed(Duration(seconds: 1), () {
if (!_updated) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(progressSnackBar());
}
});
} else if (state is AccountUpdated) {
_updated = true;
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(successSnackBar());
}
},
// rest...
),
You can do Future.delay() in your state is UpdatingAccount condition and check state again.
if (state is UpdatingAccount) {
Future.delayed(Duration(seconds: 1), (){
if(state is "UpdatingAccount"){
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(progressSnackBar());
}
});
}

Flutter application freezes after notifyListeners

I'm trying to write a simple Flutter app using the Google Maps plugin. I need to use multiple BLoC/ChangeNotifier objects in order to manage the object shown on-screen.
The issue comes out when I call notifyListeners() on a ChangeNotifier. The method which calls notifyListeners() completes its execution, and then the app freezes completely (no widget update, unable to interact with existing widgets).
I've tried to understand where's the problem: the only thing I understood is that it works fine while CompaniesData (which is the ChangeNotifier that causes the problem) is empty.
class CompaniesData extends ChangeNotifier {
Map<MarkerId, Company> _companiesMap;
set companies(Set<Company> companies) {
_companiesMap = companies != null
? Map.fromIterable(
companies,
key: (company) => MarkerId(company.id.toString()),
value: (company) => company,
)
: null;
notifyListeners();
;
}
bool get available => _companiesMap != null;
Company companyWithId(MarkerId id) => available ? _companiesMap[id] : null;
Map<MarkerId, Company> get companiesIfAvailable =>
available ? _companiesMap : Map();
Iterable<Company> companiesFromIds(BuildContext context, Set<int> ids) {
Set<int> idsCopy = Set.from(ids);
return companiesIfAvailable.entries
.where((entry) => idsCopy.remove(entry.value.id))
.map<Company>((entry) => entry.value);
}
}
#override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Consumer<CompaniesData>(
builder: (context, data, child) {
return BlocBuilder(
bloc: BlocProvider.of<ShownCompaniesBloc>(context),
builder: (context, shownCompaniesState) {
return BlocBuilder(
bloc: BlocProvider.of<FavoriteCompaniesBloc>(context),
builder: (context, favoriteCompaniesState) {
return BlocBuilder(
bloc: BlocProvider.of<MapPropertiesBloc>(context),
builder: (context, mapPropertiesState) {
CompaniesData data =
Provider.of<CompaniesData>(context, listen: false);
// ...
As you can see, the build method contains multiple nested BLoC/Consumer objects.
#override
void initState() {
_fetchCompanies();
super.initState();
}
void _fetchCompanies() {
findUser().then((location) {
Set<Company> companies = Set.from([Company.fake()]);
// CompaniesData.companies is a setter, which calls
// notifyListeners
_companiesData.companies = companies;
});
}
I don't get error messages, exception, my app simply dies after the end of the execution of the callback given to findUser().then().
EDIT:
I changed the code a little bit, and I figured out that the problem isn't notifyListeners (or at least it isn't now).
final Completer<Map<MarkerId, Company>> _companiesData = Completer();
_AeroMainViewState() {
findUser()
.then(_fetchCompanies)
.then((companies) => _companiesData.complete(Map.fromIterable(
companies,
key: (company) => MarkerId(company.id.toString()),
value: (company) => company,
)));
}
Future<Set<Company>> _fetchCompanies(LatLng location) async =>
Set.from([Company.fake()]);
// ...
child: FutureBuilder<Map<MarkerId, Company>>(
future: _companiesData.future,
builder: (context, snapshot) {
// this builder function isn't called at all
// when the Completer _companiesData is completed
if (snapshot.connectionState == ConnectionState.done) {
return Provider<Map<MarkerId, Company>>.value(
value: snapshot.data,
child: // ...
} else {
return Center(child: CircularProgressIndicator());
}
}),
// ...
Removing the ChangeNotifier doesn't fix the issue.
I post my error for future reference. I was doing this in a class:
static Stream<Obj1> stream() async* {
while (true) {
yield Obj1();
}
}
_subscription = Obj1.stream().listen((event) {
// do something...
}
Since the Stream contains potentially an infinite number of objects, the subscription to that stream was blocking the main (and only) thread.