Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant read Storage on onInit() #40

Closed
ingomc opened this issue Jan 12, 2021 · 3 comments
Closed

Cant read Storage on onInit() #40

ingomc opened this issue Jan 12, 2021 · 3 comments

Comments

@ingomc
Copy link

ingomc commented Jan 12, 2021

so i have this simple controller, and in onInit box.read('citys') is null, i dont know why, is it a bug, how do i get the value in onInit and assign it to something?

with the getter of my controller, i am able to get the value from storage.

(First time this runs, it should be null, but secondtime it should return the stored value, its there i get it from the getter)

controller.dart

import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

class TestController extends GetxController {
  final box = GetStorage();
  String get stringtext => box.read('citys') ?? "nux";

  @override
  void onInit() {
    print(box.read('citys')); // returns null ?????
    print('write');
    box.write('citys', 'Hallo!!');
    super.onInit();
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'controller.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final TestController testController = Get.put(TestController());

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: SimpleBuilder(
            builder: (_) {
              return Text(testController.stringtext);
            },
          ),
        ),
      ),
    );
  }
}
@jonataslaw
Copy link
Owner

Initialize GetStorage (read the docs!)

@ingomc
Copy link
Author

ingomc commented Jan 13, 2021

Ok forgot Initialize GetStorage and also getMaterialApp in this Demo .. sry for that... that was the Problem in the Demo, but i didn't forget it in my acutal App, and there it doesnt work with it initialized, so there is probably an error somewhere, im lookin into it, perharps i can find the error or reproduce it somehow.

Sorry for that in the demo, and thank you for the repsonse

@cwangfr
Copy link

cwangfr commented Mar 28, 2022

@ingomc
i have same problem, but i fixed use await GetStorage.init();
"await" is key point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants