I am trying to create a global class with multiple modules inside it. I am rewriting the code from Javascript to Dart.
Javascript
import ModuleA from './modules/a'
import ModuleB from './modules/b'
import ModuleC from './modules/c'
class Parent {
constructor() {
this.moduleA = new ModuleA(this)
this.moduleB = new ModuleB(this)
this.moduleC = new ModuleC(this)
}
}
Dart
import 'modules/a.dart';
import 'modules/b.dart';
import 'modules/c.dart';
class Parent {
ModuleA moduleA = ModuleA(parent: Parent());
ModuleB moduleB = ModuleB(parent: Parent());
ModuleC moduleC = ModuleC(parent: Parent());
}
After build I get this error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Stack Overflow
Your Dart class should have a constructor that initializes the members and passes this
when constructing the members:
import 'modules/a.dart';
import 'modules/b.dart';
import 'modules/c.dart';
class Parent {
Parent() {
moduleA = ModuleA(parent: this);
moduleB = ModuleB(parent: this);
moduleC = ModuleC(parent: this);
}
ModuleA moduleA;
ModuleB moduleB;
ModuleC moduleC;
}
you're not quite using classes right, please go to this link for an explanation
import ModuleA from './modules/a'
import ModuleB from './modules/b'
import ModuleC from './modules/c'
class Parent {
constructor(){
this.moduleA = new ModuleA(this);
this.moduleB = new ModuleB(this);
this.moduleC = new ModuleC(this);
}
}```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.