brianegan / scoped_model

A Widget that passes a Reactive Model to all of it's children
BSD 3-Clause "New" or "Revised" License
774 stars 76 forks source link

scoped_model issues #94

Open armelyara opened 4 years ago

armelyara commented 4 years ago

Hello, I am facing some issues with scoped_model. Can you help me to fix it.

kishan2612 commented 4 years ago

Hello, I am facing some issues with scoped_model. Can you help me to fix it.

provide more info

armelyara commented 4 years ago

Hi, I send you my errors Error: Could not find the correct Provider above this MyHome Widget

To fix, please:

If none of these solutions work, please file a bug at: https://github.com/rrousselGit/provider/issues

armelyara commented 4 years ago

I change scoped Model by Provider but I get same errors

brianegan commented 4 years ago

Hey @armelyara -- no worries, this is a common gotcha when starting with ScopedModel or Provider!

It looks like you've got one of the problems in that list, but it's hard to know which one, so let's go through in order :)

  1. Make sure you're wrapping your MaterialApp as the child of a Provider Widget.
  2. When you define your provider, do it like this: Provider<ClassIWantToProvide>(...), making sure to add the <ClassIWantToProvider> part.
  3. Then, when reading the value, make sure you're doing it like so: final myClass = Provider.of<ClassIWantToProvide>() where you make sure once again to add the <ClassIWantToProvide> in there.

If none of those work, please paste a small code sample :)

armelyara commented 4 years ago

Ok I will try it and give you feedback soon.

ARMEL YARA

COMMUNITY MANAGER | BLOGUEUR VICE-MANAGER GDG Cocody TensorFlow Abidjan Manager DSC Côte d'Ivoire Mentor Twitter :https://twitter.com/ArmelYara Blog: https://thedayinfo.com/ https://thedayinfo.blogspot.com/

Le mar. 28 avr. 2020 à 11:34, Brian Egan notifications@github.com a écrit :

Hey @armelyara https://github.com/armelyara -- no worries, this is a common gotcha when starting with ScopedModel or Provider!

It looks like you've got one of the problems in that list, but it's hard to know which one, so let's go through in order :)

  1. Make sure you're wrapping your MaterialApp as the child of a Provider Widget.
  2. When you define your provider, do it like this: Provider(...), making sure to add the part.
  3. Then, when reading the value, make sure you're doing it like so: final myClass = Provider.of() where you make sure once again to add the in there.

If none of those work, please paste a small code sample :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/brianegan/scoped_model/issues/94#issuecomment-620550407, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJPP6ZWPWNNHF47W67WXB6DRO25MZANCNFSM4MMWOFAQ .

armelyara commented 4 years ago

Hi Brienan, I got the same error. That's a small code sample

// Copyright 2019 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.

import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart';

import 'package:dr_green/camera/add_dataset_label_screen.dart'; import 'package:dr_green/camera/constants.dart'; import 'package:dr_green/camera/datasets_list.dart'; import 'package:dr_green/camera/intro_tutorial.dart'; import 'package:dr_green/camera/signin_page.dart'; import 'package:dr_green/camera/storage.dart'; import 'package:dr_green/camera/user_model.dart';

void main() async { final FirebaseStorage storage = await initStorage(STORAGE_BUCKET); final FirebaseStorage autoMlStorage = await initStorage(AUTOML_BUCKET);

runApp(new MyApp( storage: storage, autoMlStorage: autoMlStorage, userModel: UserModel(), )); }

enum MainAction { logout, viewTutorial }

class MyApp extends StatelessWidget { final FirebaseStorage storage; final FirebaseStorage autoMlStorage; final UserModel userModel;

const MyApp({ Key key, @required this.storage, @required this.autoMlStorage, @required this.userModel, }) : super(key: key);

@override Widget build(BuildContext context) { return Provider( create: (_) => userModel, child: new InheritedStorage( storage: storage, autoMlStorage: autoMlStorage, child: new MaterialApp( title: 'Custom Image Classifier', theme: new ThemeData( primaryColor: Colors.white, accentColor: Colors.deepPurple, dividerColor: Colors.black12, ), initialRoute: MyHome.routeName, routes: { MyHome.routeName: (context) => MyHome(), //IntroTutorial.routeName: (context) => IntroTutorial(), }, ), ), ); } }

class MyHome extends StatefulWidget { static const routeName = '/';

const MyHome();

@override _MyHomeState createState() => new _MyHomeState(); }

class _MyHomeState extends State { final GlobalKey _scaffoldKey = GlobalKey();

//@override //void initState() { // super.initState(); // checkAndShowTutorial(); //}

//void checkAndShowTutorial() async { //SharedPreferences prefs = await SharedPreferences.getInstance(); // final seenTutorial = prefs.getBool('seenTutorial') ?? false; // if (!seenTutorial) { // Navigator.pushNamed(context, IntroTutorial.routeName); // } else { // print("Has seen tutorial before. Skipping"); //} //}

@override Widget build(BuildContext context) { final model = Provider.of(context); final query = Firestore.instance.collection('datasets');

return new Scaffold(
  key: _scaffoldKey,
  appBar: new AppBar(
    title: new Text("Datasets"),
    actions: <Widget>[
      if (!model.isLoggedIn())
        IconButton(
          onPressed: () {
            model
                .beginSignIn()
                .then((user) => model.setLoggedInUser(user))
                .catchError((e) => print(e));
          },
          icon: Icon(
            Icons.person_outline,
          ),
        ),
      model.isLoggedIn()
          ? PopupMenuButton<MainAction>(
              onSelected: (MainAction action) {
                switch (action) {
                  case MainAction.logout:
                    model.logOut();
                    break;
                  case MainAction.viewTutorial:
                    Navigator.pushNamed(context, IntroTutorial.routeName);
                    break;
                }
              },
              itemBuilder: (BuildContext context) =>
                  <PopupMenuItem<MainAction>>[
                PopupMenuItem<MainAction>(
                  child: Text.rich(
                    TextSpan(
                      text: 'Logout',
                      children: [
                        TextSpan(
                          text: " (${model.user.displayName})",
                          style: TextStyle(
                            color: Colors.black38,
                            fontStyle: FontStyle.italic,
                          ),
                        )
                      ],
                    ),
                  ),
                  value: MainAction.logout,
                ),
                const PopupMenuItem<MainAction>(
                  child: Text('View Tutorial'),
                  value: MainAction.viewTutorial,
                )
              ],
            )
          : Container()
    ],
  ),
  body: DatasetsList(
    scaffoldKey: _scaffoldKey,
    query: model.isLoggedIn()
        ? query
        : query.where('isPublic', isEqualTo: true),
    model: model,
  ),
  floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
  // show fab button only on personal datasets page
  floatingActionButton: new FloatingActionButton.extended(
    icon: Icon(Icons.add),
    label: Text("New Dataset"),
    onPressed: () async {
      if (model.isLoggedIn()) {
        Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) =>
                  AddDatasetLabelScreen(DataKind.Dataset, "", "", ""),
            ));
      } else {
        // Route to login page
        final result = await Navigator.push(
            context, MaterialPageRoute(builder: (context) => SignInPage()));
        if (result) {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =>
                      AddDatasetLabelScreen(DataKind.Dataset, "", "", "")));
        }
      }
    },
  ),
);

} }