Hey I am building an app in flutter and when I run google sign in it gives me error that

E/flutter (30710): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe. E/flutter (30710): At this point the state of the widget's element tree is no longer stable. E/flutter (30710): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

can anyone help on this my main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends ConsumerStatefulWidget {
  const MyApp({super.key});

  @override
  ConsumerState<MyApp> createState() => _MyAppState();
}

class _MyAppState extends ConsumerState<MyApp> {
  UserModel? userModel;

  void getData(WidgetRef ref, User data) async {
    userModel = await ref
        .watch(authControllerProvider.notifier)
        .getUserData(data.uid)
        .first;
    debugPrint("cdsdvs $userModel");
    ref.read(userProvider.notifier).update((state) => userModel);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Mama App',
      debugShowCheckedModeBanner: false,
      theme: Pallete.darkModeAppTheme,
      home: ref.watch(authStateChangeProvider).when(
          data: (data) {
            if (data != null) {
              const HomeScreen();
            }
            return const LoginScreen();
          },
          error: ((error, stackTrace) => ErrorText(error: error.toString())),
          loading: () => const Loader()),
    );
  }
}
1

There are 1 best solutions below

0
Ruble On

There is probably a problem here:

void getData(WidgetRef ref, User data) async {
    userModel = await ref
        .watch(authControllerProvider.notifier)
        .getUserData(data.uid)
        .first;
    debugPrint("cdsdvs $userModel");
    ref.read(userProvider.notifier).update((state) => userModel);
    setState(() {});
  }

You should not use ref.watch in callbacks and asynchronous code. Use instead ref.read