无法从提供程序类获取值

时间:2020-01-18 02:08:37

标签: flutter provider

对不起,我是新手,我对此问题感到困惑。 我正在尝试使用提供程序包访问与changeNotifier一起使用的值, 只是打印它,但它总是返回null。

Main.dart

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    vector<float> vec;
    ifstream fin("Test.dat");
    float num;

    while (fin >> num)
        vec.push_back(num);


    for(int counti=0; counti<2 ; counti++)
    {
    cout<<vec[counti]<<endl;
    }

return 0;
}

LoginService.dart

    class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => LoginService(),
        )
      ],
      child: MaterialApp(
        title: 'Test',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            primaryColor: Colors.deepOrange, accentColor: Colors.amber),
        home: HomePage(),
        routes: {ExpenseListPage.routeName: (ctx) => ExpenseListPage()},
      ),
    );
  }
}

从登录页面导航后在新页面尝试使用的页面

class LoginService with ChangeNotifier {
  String backendUrl = 'http://10.0.2.2:3000/login';
  UserData _userData = UserData();

  // Login
  Future<bool> login({String email, String password}) async {
    var userData = {"email": email, "password": password};
    Response response = await post('$backendUrl/login', body: userData);
    String body = response.body;
    var userObject = jsonDecode(body);
    if (userObject['token'] != null) {
      setUserData(
        name: userObject['name'],
      );
      return Future.value(true);
    }
    return Future.value(false);
  }

  setUserData({String name}) {
    _userData.name = name;
    notifyListeners();
  }

  UserData getUserData() => _userData;
}

0 个答案:

没有答案