首页 > 解决方案 > 提供者无法获得价值

问题描述

我正在使用颤振构建移动应用程序。我尝试使用提供者,但它没有得到价值。

这是类更改通知程序的代码

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(),
  },
);
}

}

我总是得到“一个非空必须向文本小部件提供非空字符串。” 我认为问题是当我将值放入更改通知程序类中并尝试在其他类中获取值时,该值为空。有谁知道是什么错误??

谢谢你

标签: flutterflutter-providerflutter-change-notifier

解决方案


推荐阅读