I have an object Students on Group1.dart file

   class Group1 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
    //...
    Students('Alex','Thomas')
    //...
    }
    //My Object
    Students (String a, string b){
      //....
      }
        }

And I create Group2.dart and Group3.dart.

My question, how can I access object Students from Group1 ?

Like that

class Group2 extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
        //...
        Students('Alex','Thomas')
        //...
        }
            }

I tried create Students.dart

class ModalStudents {
String _a, _b;
ModalStudents (this._a, this._b);
String get a => _a;
String get b =>_b;} 

but I don't know what next.


Solution 1: Glup3

If I correctly understood your question, then you want to share "state". In this case your Students object. There are many state management patterns like:

  • ScopedModel
  • Redux
  • BLoC
  • ...

Then you can do whatever you want with your Students in every class :)

Other scenario: Your Widget Group2 is in Group1. Then you can just pass it as parameter ;)

Yours Glup3


Solution 2: Goblinator

You can use the scoped_model package

You want the model to extend Model

class StudentModel extends Model {
                 ...
    }

In group 1

  Widget build(BuildContext context) {
    return new ScopedModel<StudentModel>(
      model: StudentModel(),

In group 2

ScopedModelDescendant<StudentModel>(
              builder: (context, child, model){
                   ...
}

Inside the descendant you can access the properties of the model like such: model.a, model.b