_authenticatedUser变量未初始化

时间:2020-06-02 04:17:04

标签: flutter scoped-model

Future<Null> fetchCurrUser(String userId, String authToken) async {
    isLoading = true;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return http
        .get(
            'https://vyapar-fca10.firebaseio.com/users/${userId}.json?auth=${authToken}')
        .then<Null>((http.Response response) {
      print(json.decode(response.body));
      Map<String, dynamic> fetchedData = json.decode(response.body);
      this._authenticatedUser = new User(
          name: fetchedData['name'],
          email: fetchedData['email'],
          mob: fetchedData['mob'],
          userId: userId,
          token: authToken,
          designation: fetchedData['designation'],
          image: fetchedData['image']);

      notifyListeners();

      prefs.setString('name', fetchedData['name']);
      prefs.setString('mob', fetchedData['mob']);
      prefs.setString('image', fetchedData['image']);
      prefs.setString('designation', fetchedData['designation']);

      //print(_authenticatedUser.designation);
    });

但是,当我尝试在初始化后在其他dart文件中使用authenticatedUser时,它将引发错误,该函数在NULL上调用。的代码如下。

Widget build(BuildContext context) {
    // TODO: implement build
    return ScopedModel<MainModel>(
        model: new MainModel(),
        child:
            ScopedModelDescendant<MainModel>(builder: (context, child, model) {
          Widget pageContent = _buildPageContent(context);
          print("------");
          print(model.user.email);
          print(model.user.userId);
          return pageContent;
        }));
  }

0 个答案:

没有答案