flutter - 提供者无法获得价值
问题描述
我正在使用颤振构建移动应用程序。我尝试使用提供者,但它没有得到价值。
这是类更改通知程序的代码
class StoreProvider with ChangeNotifier{
UserServices _userServices = UserServices();
StoreServices _storeServices = StoreServices();
User user = FirebaseAuth.instance.currentUser;
String userLocation = "";
String selectedStore;
String selectedStoreId;
getSelectedStore(storeName, storeId){
this.selectedStore = storeName;
this.selectedStoreId = storeId;
notifyListeners();
}
}
这是我调用这个函数的代码
ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: snapshot.data.docs.map((DocumentSnapshot document) {
return InkWell(
onTap: (){
_storeData.getSelectedStore(document['shopName'], document['uid']);
pushNewScreenWithRouteSettings(
context,
settings: RouteSettings(name: VendorHomeScreen.id),
screen: VendorHomeScreen(),
withNavBar: true,
pageTransitionAnimation: PageTransitionAnimation.cupertino,
);
},
child: Padding(
padding: const EdgeInsets.only(top: 4, right: 4, left: 4),
child: Container(
width: 120,
child: Column(
children: [
SizedBox(
width: 120,
height: 100,
child: Card(
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Image.network(document['imageUrl'],))),
),
Container(
height: 35,
child: Text(document['shopName'], style: TextStyle(
fontSize: 14, fontWeight: FontWeight.bold,
), maxLines: 2, overflow: TextOverflow.ellipsis,),
),
],
),
),
),
);
}).toList(),
),
这是我想要获取我提供给提供者的值的代码
var _store = Provider.of<StoreProvider>(context);
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
iconTheme: IconThemeData(
color: Colors.white,
),
actions: [
IconButton(
onPressed: () {},
icon: Icon(CupertinoIcons.search),
)
],
title: Text(
**//this is the error**
**//this is the error**
**//this is the error**
**//this is the error**
**//this is the error**
**_store.selectedStoreId,**
style:
TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
),
)
];
},
body: Center(
child: Text('vendor screen'),
),
));
这是主文件
void main() async {
Provider.debugCheckInvalidValueType = null;
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => AuthProvider()),
ChangeNotifierProvider(create: (_) => StoreProvider()),
],
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primaryColor: Colors.lightBlueAccent, fontFamily: 'Lato'),
initialRoute: SplashScreen.id,
routes: {
SplashScreen.id: (context) => SplashScreen(),
LoginPage.id: (context) => LoginPage(),
SignupPage.id: (context) => SignupPage(),
HomePage.id: (context) => HomePage(),
ResetPassword.id: (context) => ResetPassword(),
MainScreen.id: (context)=> MainScreen(),
VendorHomeScreen.id: (context) => VendorHomeScreen(),
},
);
}
}
我总是得到“一个非空必须向文本小部件提供非空字符串。” 我认为问题是当我将值放入更改通知程序类中并尝试在其他类中获取值时,该值为空。有谁知道是什么错误??
谢谢你
解决方案
推荐阅读
- javascript - JS,如何在特定显示器上启动应用程序?
- vb.net - Visual Basic 中的简单银行应用程序
- postgresql - Docker:postgres与节点之间没有连接
- c++ - 使用 ssh 重新连接到远程 Ubuntu 机器后,如何继续使用 gdb 调试 c++ 代码?
- mysql - 如何使用我的 sql 中具有“日期格式”的列按“月份”排序?
- prolog - Prolog中列表中的重复和双倍元素
- python - Dash,如何根据单击的按钮进行回调?
- mysql - 尝试导入大型 sql 数据库文件时,DBeaver mysql 中出现错误
- python - 在 django 视图中使用时间戳范围过滤错误
- discord.js - 查找服务器的前 300 个成员