我正在通过添加Provider作为状态管理来重构我的Flutter 应用程序代码.

Desired behavior:当主屏幕打开时,应用程序应该判断用户的邮箱是否经过验证,如果没有验证,则应该显示对话框弹出窗口.

Problem:当我通过构造函数为EmailVerify传递数据时,它工作得很好,但是如果我想使用Provider,我无法在initState()个生命周期中获得这些数据.

对于这样的用例,您能推荐我正确的方法吗?

import 'package:myapp/services/authentication.dart';
import 'package:myapp/screens/settings_screen.dart';
import 'package:flutter/material.dart';
import 'package:myapp/services/authentication.dart';
import 'package:provider/provider.dart';

class HomeScreen extends StatefulWidget {

  @override
  State<StatefulWidget> createState() => new _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  bool _isEmailVerified = false;

  @override
  void initState() {
    super.initState();
    _checkEmailVerification(); // <=== Method which should show Dialog box if email is not verified which is coming from "Auth" Provider
  }

  @override
  Widget build(BuildContext context) {
    final auth = Provider.of<Auth>(context, listen: false); // <==== Service from Provider, which contains data for _isEmailVerified
    auth.isEmailVerified().then((value) => _isEmailVerified = value);

    return new Scaffold(
      appBar: new AppBar(
        title: new Text('My App'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Text(
              'Welcome to my app',
            ),
          ],
        ),
      ),
    );
  }

  void _checkEmailVerification() async {
    _isEmailVerified = auth.isEmailVerified(); // <=== How can I use "auth" from Provider to get isEmailVerified data ????
    if (!_isEmailVerified) {
      _showVerifyEmailDialog();
    }
  }

  void _showVerifyEmailDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Verify your account"),
          content: new Text("Please verify account in the link sent to email"),
          actions: <Widget>[
            new FlatButton(
              child: new Text("Resend link"),
              onPressed: () {
                Navigator.of(context).pop();
                _resentVerifyEmail();
              },
            ),
            new FlatButton(
              child: new Text("Dismiss"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  void _resentVerifyEmail() {
    // Logic to send email
  }
}

推荐答案

您需要使用上下文来调用Provider.of(),这样才能添加在第一次构建之后调用的addPostFrameCallback(),在那里您可以使用上下文

@override
void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      auth = Provider.of<Auth>(context, listen: false);
    });
}

Flutter相关问答推荐

Flutter 导致底部溢出

错误:找不到字体功能类型.Ffltter Google_Fonts包错误

我怎样才能判断一个字符串值是否为空(&Q;&Q;)?

在Android Studio的新用户界面中未找到热重新加载

video_player在Android上返回401 Unauthorized with Bunny CDN

我不能在Fighting中裁剪图片输入错误,否则应用程序会崩溃

Ffltter:发布版本中的Path NotFoundException

Flutter /dart 列表.第一个子类列表中的Where()错误

在背景图像上排列定位徽标

Ffmpeg_kit_fltter:^6.0.3版权问题

Flutter:如何设置 PersistentFooterButtons 背景?

如果其他Provider 的帐户已存在,如何阻止用户登录?

Flutter:如何在现有布局之上显示圆角布局?

如何在Flutter中动态绘制一条从A点到B点的线?

Elevated 和 Outlined 按钮之间的动画

Flutter - 如何将按钮对齐到行的右侧

我在弹出框中编辑后无法保存日期

将一个 Flutter 应用程序集成到另一个 Flutter 应用程序中

如何在 tabBar 中的选项卡之间制作垂直线?

Flutter Widget 测试:无法使用 find.byWidget() 找到小部件