'SuperController of Getx Package in Flutter causing a problem
I am facing a problem when I extends a class with SuperController of Getx PAckage in Flutter. Please go through my code and let me know the solution.
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_app/user_status_controller.dart';
import 'package:get/get.dart';
void main() {
Get.put(UserStatusController());
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: Text(
'Hello',
style: TextStyle(fontSize: 50),
)),
);
}
}
user_status_controller.dart
import 'package:get/get.dart';
class UserStatusController extends SuperController {
@override
void onDetached() {}
@override
void onInactive() {}
@override
void onPaused() {}
@override
void onResumed() {}
}
The error thrown is:
E/flutter ( 3620): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
E/flutter ( 3620): #0 FullLifeCycleMixin.onInit (package:get/get_state_manager/src/simple/get_controllers.dart:90:28)
E/flutter ( 3620): #1 GetLifeCycleBase._onStart (package:get/get_instance/src/lifecycle.dart:66:5)
E/flutter ( 3620): #2 InternalFinalCallback.call (package:get/get_instance/src/lifecycle.dart:12:26)
E/flutter ( 3620): #3 GetInstance._startController (package:get/get_instance/src/get_instance.dart:253:16)
E/flutter ( 3620): #4 GetInstance._initDependencies (package:get/get_instance/src/get_instance.dart:204:11)
E/flutter ( 3620): #5 GetInstance.find (package:get/get_instance/src/get_instance.dart:301:17)
E/flutter ( 3620): #6 GetInstance.put (package:get/get_instance/src/get_instance.dart:86:12)
E/flutter ( 3620): #7 Inst.put (package:get/get_instance/src/extension_instance.dart:89:21)
E/flutter ( 3620): #8 main (package:flutter_app/main.dart:6:7)
E/flutter ( 3620): #9 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:129:25)
E/flutter ( 3620): #10 _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 3620): #11 _CustomZone.run (dart:async/zone.dart:1328:19)
[GETX] Instance "UserStatusController" has been created
E/flutter ( 3620): #12 _runZoned (dart:async/zone.dart:1863:10)
E/flutter ( 3620): #13 runZonedGuarded (dart:async/zone.dart:1851:12)
E/flutter ( 3620): #14 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:125:5)
E/flutter ( 3620): #15 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter ( 3620): #16 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
Could anybody tell me what is throwing the error. Please help me. This is a simple code I'm unable to understand what is causing this.
Solution 1:[1]
hmm try this if its working if not mention me
xample your main.dart
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: HomePage(),
getPages: RouteList.routeLists,
initialRoute: "/home"
);
}
}
create a binding dart named it binding_home.dart depends on you
class YourBinding extends Bindings {
@override
void dependencies() {
// TODO: implement dependencies
Get.lazyPut(() => UserStatusController (), fenix: true);
}
}
then on your route to bind it
class RouteList {
static final routeLists = [
GetPage(
transition: Transition.cupertinoDialog,
transitionDuration: 350.milliseconds,
name: "/home",
binding: YourBinding(),
page: () => const HomePage()),
];
}
next is your controller
class UserStatusController extends SuperController {
@override
void onDetached() {}
@override
void onInactive() {}
@override
void onPaused() {}
@override
void onResumed() {}
}
then lastly on homepage try use Getview
class HomePage extends GetView<UserStatusController > {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: Text(
'Hello',
style: TextStyle(fontSize: 50),
)),
);
}
}
i do sometime it depends use Getview so that i wont put on the stateless like Get.put() or find(). as per default calling on the controller just type controller.yourValue
Solution 2:[2]
In order to use SuperController you need to call WidgetsFlutterBinding.ensureInitialized(); on the main function of your app:
void main(){
WidgetsFlutterBinding.ensureInitialized();
runApp(YourApp());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arbiter Chil |
| Solution 2 | S. M. JAHANGIR |
