Flutter - 如何将用户数据传递给所有视图

问题描述 投票:21回答:3

我是一个新的移动应用程序开发的新手,并且在我应用程序中如何传递用户数据时遇到了困难。

我已经尝试了几件事,但似乎都没有,我确信我应该遵循最好的练习模式。

因为它使示例更容易,我使用firebase进行身份验证。我目前有一个单独的登录路径。一旦我登录,我想在大多数视图中使用用户模型来检查显示内容的权限,在抽屉中显示用户信息等...

Firebase有一个await firebaseAuth.currentUser();最好的做法是在你可能需要用户的地方调用它吗?如果是的话,这个电话的最佳位置在哪里?

flutter codelab显示了一个很好的例子,在允许写入之前验证用户。但是,如果页面需要检查auth以确定要构建的内容,则异步调用不能进入build方法。

INITSTATE

我尝试过的一种方法是覆盖initState并启动调用以获取用户。当未来完成时,我打电话给setState并更新用户。

    FirebaseUser user;

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

  Future<Null> _getUserDetail() async {
    User currentUser = await firebaseAuth.currentUser();
    setState(() => user = currentUser);
  }

这很有效,但似乎需要它的每个小部件的很多仪式。屏幕在没有用户的情况下加载时也会闪烁,然后在未来完成时与用户进行更新。

通过构造函数传递用户

这也有效,但是有很多样板可以让用户通过可能需要访问它们的所有路由,视图和状态。此外,我们不能只在转换路线时执行popAndPushNamed,因为我们无法将变量传递给它。我们必须更改类似于此的路线:

Navigator.push(context, new MaterialPageRoute(
    builder: (BuildContext context) => new MyPage(user),
));

继承的小部件

https://medium.com/@mehmetf_71205/inheriting-widgets-b7ac56dbbeb1

这篇文章展示了使用InheritedWidget的一个很好的模式。当我将继承的小部件放在MaterialApp级别时,当auth状态改变时,子节点不会更新(我确定我做错了)

  FirebaseUser user;

  Future<Null> didChangeDependency() async {
    super.didChangeDependencies();
    User currentUser = await firebaseAuth.currentUser();
    setState(() => user = currentUser);
  }

  @override
  Widget build(BuildContext context) {
    return new UserContext(
      user,
      child: new MaterialApp(
        title: 'TC Stream',
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new LoginView(title: 'TC Stream Login', analytics: analytics),
        routes: routes,
      ),
    );
  }

FutureBuilder

FutureBuilder似乎也是一个不错的选择,但似乎每条路线都有很多工作要做。在下面的部分示例中,_authenticateUser()在完成时获取用户并设置状态。

  @override
  Widget build(BuildContext context) {
    return new FutureBuilder<FirebaseUser>(
      future: _authenticateUser(),
      builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return _buildProgressIndicator();
        }
        if (snapshot.connectionState == ConnectionState.done) {
          return _buildPage();
        }
      },
    );
  }

我很感激有关最佳实践模式的建议或用于示例的资源链接。

dart firebase-authentication flutter
3个回答
15
投票

我建议进一步调查继承的小部件;下面的代码显示了如何使用它们异步更新数据:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(new MaterialApp(
      title: 'Inherited Widgets Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new Scaffold(
          appBar: new AppBar(
            title: new Text('Inherited Widget Example'),
          ),
          body: new NamePage())));
}

// Inherited widget for managing a name
class NameInheritedWidget extends InheritedWidget {
  const NameInheritedWidget({
    Key key,
    this.name,
    Widget child}) : super(key: key, child: child);

  final String name;

  @override
  bool updateShouldNotify(NameInheritedWidget old) {
    print('In updateShouldNotify');
    return name != old.name;
  }

  static NameInheritedWidget of(BuildContext context) {
    // You could also just directly return the name here
    // as there's only one field
    return context.inheritFromWidgetOfExactType(NameInheritedWidget);
  }
}

// Stateful widget for managing name data
class NamePage extends StatefulWidget {
  @override
  _NamePageState createState() => new _NamePageState();
}

// State for managing fetching name data over HTTP
class _NamePageState extends State<NamePage> {
  String name = 'Placeholder';

  // Fetch a name asynchonously over HTTP
  _get() async {
    var res = await http.get('https://jsonplaceholder.typicode.com/users');
    var name = JSON.decode(res.body)[0]['name'];
    setState(() => this.name = name); 
  }

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

  @override
  Widget build(BuildContext context) {
    return new NameInheritedWidget(
      name: name,
      child: const IntermediateWidget()
    );
  }
}

// Intermediate widget to show how inherited widgets
// can propagate changes down the widget tree
class IntermediateWidget extends StatelessWidget {
  // Using a const constructor makes the widget cacheable
  const IntermediateWidget();

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new Padding(
        padding: new EdgeInsets.all(10.0),
        child: const NameWidget()));
  }
}

class NameWidget extends StatelessWidget {
  const NameWidget();

  @override
  Widget build(BuildContext context) {
    final inheritedWidget = NameInheritedWidget.of(context);
    return new Text(
      inheritedWidget.name,
      style: Theme.of(context).textTheme.display1,
    );
  }
}

0
投票

因为这个问题,我遇到了另一个问题you can check it out here所以我提出的解决方案有点凌乱,我创建了一个单独的Instance dart页面并将其导入到每个页面。

 GoogleSignInAccount Guser = googleSignIn.currentUser;
 FirebaseUser Fuser;

我在登录时将用户存储在那里并检查每个StateWidget是否为null

  Future<Null> _ensureLoggedIn() async {

if (Guser == null) Guser = await googleSignIn.signInSilently();
if (Fuser == null) {
  await googleSignIn.signIn();
  analytics.logLogin();
}
if (await auth.currentUser() == null) {
  GoogleSignInAuthentication credentials =
  await googleSignIn.currentUser.authentication;
  await auth.signInWithGoogle(
    idToken: credentials.idToken,
    accessToken: credentials.accessToken,
  );
}

这是我的旧代码,我在我当前的应用程序上清理了它,但我现在没有那个代码。只需检查null用户并再次登录

我也为大多数Firebase实例做了这个,因为我的应用程序上有超过3个页面而且Inherited Widgets工作太多了


-1
投票

对于我懒惰的mathod,我只是创建像userdata.dart这样的新文件,然后在其上放置任何变量,例如动态Profile = null

在userdata.dart中

//only put this or anything u want.
dynamic Profile = null;

在startingpage.dart

//import that file
import '../userdata.dart';

class startingpage extends ...{
...
//set data to store..
   Profile = 'user profile';
...
}

使用数据只是在anotherpage.dart中声明和使用

//import that file
import '../userdata.dart';

class anotherpage extends...{
...
}

class .. State ...{
...
//set the data to variable
   dynamic userdata = Profile;
   print('this is my lazy pass data' + userdata.toString());
...
}
© www.soinside.com 2019 - 2024. All rights reserved.