所以我正在使用提供程序和更改通知程序,问题是当我更改状态并在同一文件中通知侦听器时,用户界面已更新,但是当我尝试从另一个屏幕中的模型访问相同数据时,它仍然保持原始或第一个版本而不是更新版本,另一个文件中的类甚至使用更改通知程序提供程序和使用方,就像其中包含模态类但不更改值的文件一样,它仅保留初始值,第二个文件仅具有changenotifierprovider和consumer,但仅显示初始文件而不显示已更新的文件,而是显示具有模型类的第一个文件,并且更改通知程序功能在其小部件中显示更新的版本
答案 0 :(得分:0)
这是我通常使用Provider共享数据的方式,从一个窗口小部件中的一个窗口小部件一直到窗口小部件树的整个其他区域中的其他窗口小部件。
例如:
import 'package:flutter/material.dart';
import 'package:app/utils/AppUser.dart';
class UserProvider extends ChangeNotifier {
/// You can either set an initial value here or use a UserProvider object
/// and call the setter to give it an initial value somewhere in your app, like in main.dart
AppUser _user; /// or AppUser _user = AppUser("firstName", "lastName");
AppUser get user => _user;
set user(AppUser newUser) {
_user = newUser;
/// MAKE SURE YOU NOTIFY LISTENERS IN YOUR SETTER
notifyListeners();
}
}
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
MaterialApp(
initialRoute: '/root',
routes: {
'/root': (context) => MyApp(),
},
title: "Your App Title",
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/// Multiprovider allows you to have more than one provider, which is great for more
/// complex apps where you might need to share data about something else than just the user
return MultiProvider(
providers: [
/// THIS WILL MAKE SURE ALL WIDGETS IN YOUR APP, THAT LISTEN TO THE PROVIDER CHANGES,
/// WILL BE NOTIFIED AND THE SHARED DATA WILL BE PROVIDED TO THEM
ChangeNotifierProvider<UserProvider>.value(value: UserProvider()),
],
child: MaterialApp(
home: Wrapper(),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
/// MAKE SURE TO IMPORT THE UserProvider.dart file
import 'package:app/services/UserProvider.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
UserProvider userProvider;
@override
Widget build(BuildContext context) {
/// LISTEN TO THE CHANGES / UPDATES IN THE PROVIDER
userProvider = Provider.of<UserProvider>(context);
return Scaffold(
body: SafeArea(
child: Center(
child: Text(
/// THIS IS HOW YOU GET THE UPDATED DATA FROM THE PROVIDER
userProvider.user.firstName,
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
/// MAKE SURE TO IMPORT THE UserProvider.dart file
import 'package:app/services/UserProvider.dart';
class AnotherScreen extends StatefulWidget {
@override
_AnotherScreenState createState() => _AnotherScreenState();
}
class _AnotherScreenState extends State<AnotherScreen> {
UserProvider userProvider;
@override
Widget build(BuildContext context) {
/// LISTEN TO THE CHANGES / UPDATES IN THE PROVIDER
userProvider = Provider.of<UserProvider>(context);
return Scaffold(
body: SafeArea(
child: Center(
child: RaisedButton(
/// THIS IS HOW YOU UPDATE THE DATA IN THE PROVIDER
onPressed: () {
userProvider.user = AppUser("new", "user");
setState(() {});
},
child: Text("Update user"),
),
),
),
);
}
}
希望这会帮助您解决问题。