【问题标题】:Flutter state management with Provider使用 Provider 进行 Flutter 状态管理
【发布时间】:2019-05-20 09:35:30
【问题描述】:

我有一个简单的Provider 类:

import 'package:flutter/foundation.dart';

class AppState with ChangeNotifier {
  bool _isLoggedIn = false;
  bool get isLoggedIn => _isLoggedIn;

  set isLoggedIn(bool newValue) {
    _isLoggedIn = newValue;
    notifyListeners();
  }
}

如果登录成功,我只是在登录类中将isLoggedIn 设置为true:

 void _signInWithEmailAndPassword(appState) async {
    try {
      final FirebaseUser user = await _auth.signInWithEmailAndPassword(
        ...
      );

      if (user != null) {
        appState.isLoggedIn = true;
        appState.userData = user.providerData;
        ...
      }
    } catch (e) {
      setState(() {
        _errorMessage = e.message;
      });
    }
  }

在Android上按下后退按钮可以让用户在成功登录后返回此页面。所以我想知道Provider.of是否可以在Widget build之前访问,如果isLoggedIntrue,则重定向用户.

现在我有类似的东西:

@override
  Widget build(BuildContext context) {
    final appState = Provider.of<AppState>(context);
...

这只是登录视图的一个用例,但我确信此功能可以用于其他情况。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    如果您要在整个应用程序中使用 FirebaseUser 或登录用户,我建议您在应用程序的最高级别添加提供程序。示例

    void main() {
    
       runApp(MyApp());
       }
    
    class MyApp extends StatelessWidget {
      MyApp();
    
      @override
      Widget build(BuildContext context) {
        return MultiProvider(
          providers: [
            StreamProvider<FirebaseUser>.value(
              stream: FirebaseAuth.instance.onAuthStateChanged, // Provider here
            ),
          ],
          child: MaterialApp(
            title: 'My App',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primaryColor: Colors.green,
              primarySwatch: Colors.green,
              accentColor: Colors.yellow,
            ),
            home: MainPage(),
          ),
        );
      }
    }
    
    class MainPage extends StatefulWidget {
      MainPage({Key key, this.storage}) : super(key: key);
      final FirebaseStorage storage;
      @override
      _MainPageState createState() => _MainPageState();
    }
    
    class _MainPageState extends State<MainPage>
        with SingleTickerProviderStateMixin {
      @override
      Widget build(BuildContext context) {
        final user = Provider.of<FirebaseUser>(context); // gets the firebase user
        bool loggedIn = user != null;
    
        return loggedIn ? HomePage() : LoginPage(); // Will check if the user is logged in. And will change anywhere in the app if the user logs in
      }
    }
    

    参考文献

    Fireship 185 Provider

    Great Youtube video explaining the code

    【讨论】:

    • 谢谢。所以我让它工作了......但是如何在 Firebase 身份验证提供程序旁边添加我的AppState
    • 我明白了...我可以做到ChangeNotifierProvider&lt;AppState&gt;.value(notifier: AppState(),),
    • 抱歉回复晚了,我用了这个,不知道这个和你的有什么区别ChangeNotifierProvider(builder: (context) =&gt; LoadingService(),),
    • 酷。谢谢你的资源。我想它是双向的。也许有人会解释其中的区别。
    • 如果我们使用导航路线,如何使用这个逻辑?
    猜你喜欢
    • 2023-03-14
    • 2021-01-15
    • 2021-08-27
    • 2021-02-15
    • 2021-12-23
    • 2021-05-30
    • 2020-09-29
    • 2020-05-01
    • 2021-02-10
    相关资源
    最近更新 更多