dartflutterstatescoped-model

unable to use scoped model in flutter bottom sheet,dialogs


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

Solution

  • 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