Mahmoud Mabrouk
Mahmoud Mabrouk

Reputation: 733

Error: Could not find the correct ScopedModel

I am developing an app with many pages, and to manage states I started to use ScopedModel, here is my app simple structure:

enter image description here

my MainModel() is:

class MainModel extends Model with LoginModel, PostsFeedModel, SocialLoginModel, ProfileModel{}

and I added it to the main material app like this:

return ScopedModel<MainModel>(
  model: MainModel(),
  child: new MaterialApp(

I can't simply use ScopedModelDescendant on any of the child views like :

child: ScopedModelDescendant<MainModel>( ... I get this error

Error: Could not find the correct ScopedModel

I have to add ScopedModel<MainModel>(... to make it work!

I sow this answer here but I am pretty sure this is possible as I am following a course which it's lecturer uses a very similar structure (pushing new views) and it worked for him.

also as another try to use multiple scoped model I followed scoped model owner's suggestion here as here:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:Sheer/util/SheerDialog.dart';
import 'package:scoped_model/scoped_model.dart';
import '../../models/profile-model.dart';

class Profile extends StatefulWidget {
  final uid;

  Profile({@required this.uid});

  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
@override
  Widget build(BuildContext context) {
    final profileModel = ScopedModel.of<ProfileModel>(context);
    return profileModel.user.photoURL != ''
      ? Scaffold( ....

and it still showing the same error, any help will be appreciated. thanks and excuse my weak language.

Upvotes: 1

Views: 1935

Answers (1)

Mahmoud Mabrouk
Mahmoud Mabrouk

Reputation: 733

The issue was in the way I import the main_model.dart;

in the main.dart I imported it like that : models/main_model.dart

in the inner pages I imported it like that : package:MYAPP/models/main_model.dart

when I unified the way I called the file, it worked as expected

Thanks to Raof Rahich for his comment

Upvotes: 2

Related Questions