Search code examples
flutterflutter-layoutflutter-futurebuilder

Receiving error "setState() or markNeedsBuild() called during build"


I am receiving this error "setState() or markNeedsBuild() called during build" in this dart file. Here I am calling a Futurebuilder to getUser() authentication info, basically I am checking whether user is logged in or not and after that I am moving to respctive screens according.

I am pasting the code for reference. Thanks.

import 'package:flutter/material.dart';
import 'package:mukti/ui_pages/login_signup/login_screen.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;
import 'package:mukti/ui_pages/main_screen/main_screen.dart';
import 'package:provider/provider.dart';

import 'authService.dart';

class CheckAuthentication extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: FutureBuilder(
        future: Provider.of<AuthService>(context, listen: false).getUser(),
        builder: (context, AsyncSnapshot<auth.User> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.error != null) {
              print("error");
              return Text(snapshot.error.toString());
            }
            return snapshot.hasData ? MainScreen(firebaseUser: snapshot.data) : LoginScreen();
          } else {
            return LoadingCircle();
          }
        }
      )
    );
  }
}

class LoadingCircle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: CircularProgressIndicator(
          backgroundColor: Theme.of(context).colorScheme.primaryVariant,
        ),
        alignment: Alignment(0.0, 0.0),
      ),
    );
  }
}

Exception Message for Reference

════════ Exception caught by foundation library ════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for AuthService:
setState() or markNeedsBuild() called during build.

This _InheritedProviderScope<AuthService> widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _InheritedProviderScope<AuthService>
  value: Instance of 'AuthService'
  listening to value
The widget which was currently being built when the offending call was made was: CheckAuthentication
  dirty
When the exception was thrown, this was the stack: 
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4292:11)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4307:6)
#2      _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:496:5)
#3      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:226:25)
#4      AuthService.getUser (package:mukti/authentication/authService.dart:18:7)
...
The AuthService sending notification was: Instance of 'AuthService'
════════════════════════════════════════════════════════════════════════════════════════════════════

Solution

  • Please try this code instead:

    import 'package:flutter/material.dart';
    import 'package:mukti/ui_pages/login_signup/login_screen.dart';
    import 'package:firebase_auth/firebase_auth.dart' as auth;
    import 'package:mukti/ui_pages/main_screen/main_screen.dart';
    import 'package:provider/provider.dart';
    
    import 'authService.dart';
    
    class CheckAuthentication extends StatelessWidget {
    
    
      @override
      Widget build(BuildContext context) {
        final authService = Provider.of<AuthService>(context, listen: false);
    
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: FutureBuilder(
            future: authService.getUser(),
            builder: (context, AsyncSnapshot<auth.User> snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                if (snapshot.error != null) {
                  print("error");
                  return Text(snapshot.error.toString());
                }
                return snapshot.hasData ? MainScreen(firebaseUser: snapshot.data) : LoginScreen();
              } else {
                return LoadingCircle();
              }
            }
          )
        );
      }
    }
    
    class LoadingCircle extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Container(
            child: CircularProgressIndicator(
              backgroundColor: Theme.of(context).colorScheme.primaryVariant,
            ),
            alignment: Alignment(0.0, 0.0),
          ),
        );
      }
    }