前提・実現したいこと
emailProviderに、メールアドレスを保存しておきたいです。
理由は、ほかのWidgetで値を参照してユーザー認証をするために必要だからです。
発生している問題・エラーメッセージ
StateNotifierListenerError (At least listener of the StateNotifier Instance of 'StateController<String>' threw an exception when the notifier tried to update its state. The exceptions thrown are: setState() or markNeedsBuild() called during build. This UncontrolledProviderScope 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: UncontrolledProviderScope The widget which was currently being built when the offending call was made was: StreamBuilder<User?>
該当のソースコード
Dart
1import 'package:flutter/material.dart'; 2import 'package:firebase_core/firebase_core.dart'; 3import 'package:hooks_riverpod/hooks_riverpod.dart'; 4import 'package:sample/video/video_page.dart'; 5import 'login/login_page.dart'; 6import 'menu/programs_wiget.dart'; 7import 'package:firebase_auth/firebase_auth.dart'; 8 9final emailProvider = StateProvider((ref) { 10 return ''; 11}); 12void main() async { 13 WidgetsFlutterBinding.ensureInitialized(); 14 await Firebase.initializeApp(); 15 runApp( 16 const ProviderScope(child: Root()), 17 ); 18} 19 20class Root extends StatelessWidget { 21 const Root({Key? key}) : super(key: key); 22 23 Widget build(BuildContext context) { 24 return MaterialApp( 25 title: "Pygmalion", 26 theme: ThemeData( 27 fontFamily: "MPLUS2", 28 ), 29 home: StreamBuilder<User?>( 30 stream: FirebaseAuth.instance.authStateChanges(), 31 builder: (context, snapshot) { 32 if (snapshot.connectionState == ConnectionState.waiting) { 33 return const SizedBox(); 34 } 35 if (snapshot.hasData) { 36 context.read(emailProvider).state = "sample@gmail.com"; 37 38 return const ProgramWidget(); 39 } 40 41 return LoginPage(); 42 }, 43 ), 44 ); 45 } 46} 47
試したこと
emailproviderに、"sample@gmail.com"を代入したいです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。