unable to use scoped model in flutter bottom sheet,dialogs

741

Solution 1

Scoped Model when wrapped this to a page then it only applicable to that page only. bottom sheets and dialogs in flutter nothing but creating new context i.e a new page so either we have to wrap on top of the material app or wrap another scoped model on bottom sheet with same model. that should work. and in Ur code U have that onpressed calling a fuction in a model i think so. then the model builds again and again so change it to like this

onpressed:(){
call funtion;
pop;
}

i think it may be helpful for some might be

Solution 2

At the root of your app you need to start it using. This allows you to use the ScopedModelDescendent widgets lower down in the tree.

ScopedModel<AppModel>(
    model: AppModel(),
    child: Resources())

Wherever you're displaying your Resources from make sure it's wrapped like above then you'll be able to use that model lower in your tree.

Share:
741
Saikumarreddy2391
Author by

Saikumarreddy2391

Updated on December 02, 2022

Comments

  • Saikumarreddy2391
    Saikumarreddy2391 over 1 year

    when i run this code it shows an error that correct scopedmodel was not found. i think we have to declare another scoped model for bottomsheet,dialogs also did that using with same model but it behaving abnormally. how do i achive that how to use scoped models in such bottom sheets and dialogs.

    i was noob at scoped model any help apreciated

        import 'package:flutter/material.dart';
        import 'package:scoped_model/scoped_model.dart';
        import 'package:cloud_firestore/cloud_firestore.dart';
    
    
        class ResourcesModel extends Model{
          String selectsubject = 'Select the subject';
          List<String> sublist=[];
          change(int index){
            debugPrint('${sublist[index]}');
            selectsubject=sublist[index];
            notifyListeners();
          }
          fetchsubjects() {
          Firestore.instance.collection("resources").document("17csea").get().then((DocumentSnapshot ds){
            for (var item in ds['subjects']) {
              sublist.add(item);
            }
            notifyListeners();
          });
          }
        }
    
        class Resources extends StatelessWidget {
        final ResourcesModel resourcesModel =ResourcesModel();
    
        void showbottomsheet(context) async{
          double height =MediaQuery.of(context).size.height;
        await showModalBottomSheet(
          context: context,
          builder: (context){
            return Container(
                height: height/2,
                child: ScopedModelDescendant<ResourcesModel>(
                  builder:(context,_,model){ 
                    debugPrint('helelel');
                    return (model.sublist.isEmpty)?Center(child:CircularProgressIndicator()):
                    ListView.separated(
                      itemCount: model.sublist.length,
                      separatorBuilder: (context,_){
                        return Divider(
                          color: Theme.of(context).primaryColor,
                        );
                      },
                      itemBuilder: (context,index){
                        return ListTile(
                          title: Text(model.sublist[index]),
                          onTap: model.change(index),
                        );
                      },
                    );
                  }
                ),
              );
          }
        );
        }
          @override
          Widget build(BuildContext context) {
            return ScpedModel<ResourcesModel>(
            model:resourcesmodel,
            chilld:ScopedModelDescendant<ResourcesModel>(
    
                        builder:(context,_,model){ 
                          return Container(
                color: Color(0xFFF3F3F3),
                child: RaisedButton(
              child: Text(model.selectsubject),
              onPressed: (){
                if(resourcesModel.sublist.isEmpty){
                  resourcesModel.fetchsubjects();
                } 
                },
                ),
              );
                        }
            ),
           ); 
          }
        }
    
  • Saikumarreddy2391
    Saikumarreddy2391 about 5 years
    i am sorry i edited my question I already had it on the top of the widget tree also same error . the error is showing in the bottom sheet saying couldn't find the correct scoped model once try to run this code in ur editor to get me
  • Filled Stacks
    Filled Stacks about 5 years
    @Saikumarreddy2391 Not in your resources the root that's displaying your Resources widget. Where are you showing the resources widget? What is putting it on the screen. The place where you're putting it on the screen should be surrounded by the code I have in my answer. The child that you want to be a descendent should only be a descendent, it shouldn't provide it's own model. That's the whole point of using scoped model.
  • Saikumarreddy2391
    Saikumarreddy2391 about 5 years
    i am using the tab bar and the whole code above was the 2nd tab
  • Filled Stacks
    Filled Stacks about 5 years
    Okay.Can you post the code that adds Resources onto the screen.
  • Filled Stacks
    Filled Stacks about 5 years
    @Saikumarreddy2391 you don't seem to be understanding what I'm asking so I won't be able to help you. I hope you get this sorted. And incase you'll understand it I'll try one more time. The place where you add your Resources widget is where you need to supply the ScopedModel. NOT inside the resources view. That's too far down the tree. The place where you put your resources in your widget tree is where you should supply the ScopedModel NOT in your resources widget, that's too far down the tree.
  • Filled Stacks
    Filled Stacks about 5 years
    @Saikumarreddy2391 If you're stlll struggling with the concept I highly recommend building your own inherited to understand why ScopedModel exists.
  • Saikumarreddy2391
    Saikumarreddy2391 about 5 years
    K my widget tree is first a scaffold then tabbarview then there are 3 tabs the above code is of 2nd tab
  • Saikumarreddy2391
    Saikumarreddy2391 about 5 years
    Should i add scopedmodel above the tab bar or scaffold
  • Saikumarreddy2391
    Saikumarreddy2391 about 5 years
    i have wrapped only 2nd tab with scoped model is this a correct way or should wrap above the scaffold and tabbar.