how to change app theme in runtime in Flutter?











up vote
1
down vote

favorite












In my simple project , there is a button to change theme form light to dark and vice versa as below :



import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
}
}

class MyAppState extends State<MyApp> {
Future<bool> load(String key, bool defaultValue) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool state = prefs.getBool(key) ?? defaultValue;
return state;
}

Future<void> save(String key, bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(key, value);
}

bool state;

@override
Widget build(BuildContext context) {
load("themeKey", false).then((bool value) {
state = value;
});
debugPrint(
"..............state is : " + state.toString() + "...............");
return MaterialApp(
theme: state ? ThemeData.light() : ThemeData.dark(),
home: Scaffold(
body: Container(
child: RaisedButton(
child: Text("change"),
onPressed: () {
setState(() {
state = !state;
});
save("themeKey", state);
},
),
),
),
);
}
}


I use shared_preferences plugin to save the current theme state for the next app launch , but there is a problem it seems that app loads before shared preference loads so i am getting this exception:



Launching libmain.dart on C1905 in debug mode...
Built buildappoutputsapkdebugapp-debug.apk.
I/flutter ( 8045): ..............state is : null...............
I/flutter ( 8045): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 8045): The following assertion was thrown building MyApp(dirty, state: MyAppState#6774f):
I/flutter ( 8045): Failed assertion: boolean expression must not be null
I/flutter ( 8045): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 8045): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 8045): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 8045): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 8045): When the exception was thrown, this was the stack:
I/flutter ( 8045): #0 MyAppState.build (package:test_shared_preferenced/main.dart:36:14)
I/flutter ( 8045): #1 StatefulElement.build (package:flutter/src/widgets/framework.dart:3787:27)
I/flutter ( 8045): #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3699:15)
I/flutter ( 8045): #3 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 8045): #4 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3679:5)
I/flutter ( 8045): #5 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3826:11)
I/flutter ( 8045): #6 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3674:5)
I/flutter ( 8045): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 8045): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 8045): #9 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:909:16)
I/flutter ( 8045): #10 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:880:5)
I/flutter ( 8045): #11 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:826:17)
I/flutter ( 8045): #12 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 8045): #13 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:825:13)
I/flutter ( 8045): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:712:7)
I/flutter ( 8045): #15 runApp (package:flutter/src/widgets/binding.dart:756:7)
I/flutter ( 8045): #16 main (package:test_shared_preferenced/main.dart:5:16)
I/flutter ( 8045): #17 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
I/flutter ( 8045): #18 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 8045): ════════════════════════════════════════════════════════════════════════════════════════════════════


so what should i do in this situation , please help










share|improve this question






















  • state is return null, change condition to: Dart return MaterialApp( theme: state == null ? ThemeData.light() : ThemeData.dark(), ...)
    – EdHuamani
    Nov 19 at 17:45










  • @EdgarHuamani What you are trying to suggest will prevent the null value error, but not properly take into account the user's theme preference. The vast majority of the time state will not have been set before this code is executed. This means it will default to the light theme regardless of how the user preference is set. It would be better to set the theme once load future completes and the theme preference is known.
    – Stephen
    Nov 19 at 17:52










  • I want to make execution wait for load method to finish , how can i achieve this ,please
    – Hasan
    Nov 19 at 17:58










  • @Stephen I have only answered how to solve the error that the log shows. You're right, the ThemeData.light () will always be selected, @Hasan you can choose to use FutureBuilder I think you already have a response below.
    – EdHuamani
    Nov 19 at 18:23










  • ok , thanks a lot
    – Hasan
    Nov 19 at 19:05















up vote
1
down vote

favorite












In my simple project , there is a button to change theme form light to dark and vice versa as below :



import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
}
}

class MyAppState extends State<MyApp> {
Future<bool> load(String key, bool defaultValue) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool state = prefs.getBool(key) ?? defaultValue;
return state;
}

Future<void> save(String key, bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(key, value);
}

bool state;

@override
Widget build(BuildContext context) {
load("themeKey", false).then((bool value) {
state = value;
});
debugPrint(
"..............state is : " + state.toString() + "...............");
return MaterialApp(
theme: state ? ThemeData.light() : ThemeData.dark(),
home: Scaffold(
body: Container(
child: RaisedButton(
child: Text("change"),
onPressed: () {
setState(() {
state = !state;
});
save("themeKey", state);
},
),
),
),
);
}
}


I use shared_preferences plugin to save the current theme state for the next app launch , but there is a problem it seems that app loads before shared preference loads so i am getting this exception:



Launching libmain.dart on C1905 in debug mode...
Built buildappoutputsapkdebugapp-debug.apk.
I/flutter ( 8045): ..............state is : null...............
I/flutter ( 8045): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 8045): The following assertion was thrown building MyApp(dirty, state: MyAppState#6774f):
I/flutter ( 8045): Failed assertion: boolean expression must not be null
I/flutter ( 8045): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 8045): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 8045): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 8045): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 8045): When the exception was thrown, this was the stack:
I/flutter ( 8045): #0 MyAppState.build (package:test_shared_preferenced/main.dart:36:14)
I/flutter ( 8045): #1 StatefulElement.build (package:flutter/src/widgets/framework.dart:3787:27)
I/flutter ( 8045): #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3699:15)
I/flutter ( 8045): #3 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 8045): #4 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3679:5)
I/flutter ( 8045): #5 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3826:11)
I/flutter ( 8045): #6 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3674:5)
I/flutter ( 8045): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 8045): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 8045): #9 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:909:16)
I/flutter ( 8045): #10 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:880:5)
I/flutter ( 8045): #11 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:826:17)
I/flutter ( 8045): #12 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 8045): #13 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:825:13)
I/flutter ( 8045): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:712:7)
I/flutter ( 8045): #15 runApp (package:flutter/src/widgets/binding.dart:756:7)
I/flutter ( 8045): #16 main (package:test_shared_preferenced/main.dart:5:16)
I/flutter ( 8045): #17 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
I/flutter ( 8045): #18 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 8045): ════════════════════════════════════════════════════════════════════════════════════════════════════


so what should i do in this situation , please help










share|improve this question






















  • state is return null, change condition to: Dart return MaterialApp( theme: state == null ? ThemeData.light() : ThemeData.dark(), ...)
    – EdHuamani
    Nov 19 at 17:45










  • @EdgarHuamani What you are trying to suggest will prevent the null value error, but not properly take into account the user's theme preference. The vast majority of the time state will not have been set before this code is executed. This means it will default to the light theme regardless of how the user preference is set. It would be better to set the theme once load future completes and the theme preference is known.
    – Stephen
    Nov 19 at 17:52










  • I want to make execution wait for load method to finish , how can i achieve this ,please
    – Hasan
    Nov 19 at 17:58










  • @Stephen I have only answered how to solve the error that the log shows. You're right, the ThemeData.light () will always be selected, @Hasan you can choose to use FutureBuilder I think you already have a response below.
    – EdHuamani
    Nov 19 at 18:23










  • ok , thanks a lot
    – Hasan
    Nov 19 at 19:05













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In my simple project , there is a button to change theme form light to dark and vice versa as below :



import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
}
}

class MyAppState extends State<MyApp> {
Future<bool> load(String key, bool defaultValue) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool state = prefs.getBool(key) ?? defaultValue;
return state;
}

Future<void> save(String key, bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(key, value);
}

bool state;

@override
Widget build(BuildContext context) {
load("themeKey", false).then((bool value) {
state = value;
});
debugPrint(
"..............state is : " + state.toString() + "...............");
return MaterialApp(
theme: state ? ThemeData.light() : ThemeData.dark(),
home: Scaffold(
body: Container(
child: RaisedButton(
child: Text("change"),
onPressed: () {
setState(() {
state = !state;
});
save("themeKey", state);
},
),
),
),
);
}
}


I use shared_preferences plugin to save the current theme state for the next app launch , but there is a problem it seems that app loads before shared preference loads so i am getting this exception:



Launching libmain.dart on C1905 in debug mode...
Built buildappoutputsapkdebugapp-debug.apk.
I/flutter ( 8045): ..............state is : null...............
I/flutter ( 8045): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 8045): The following assertion was thrown building MyApp(dirty, state: MyAppState#6774f):
I/flutter ( 8045): Failed assertion: boolean expression must not be null
I/flutter ( 8045): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 8045): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 8045): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 8045): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 8045): When the exception was thrown, this was the stack:
I/flutter ( 8045): #0 MyAppState.build (package:test_shared_preferenced/main.dart:36:14)
I/flutter ( 8045): #1 StatefulElement.build (package:flutter/src/widgets/framework.dart:3787:27)
I/flutter ( 8045): #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3699:15)
I/flutter ( 8045): #3 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 8045): #4 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3679:5)
I/flutter ( 8045): #5 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3826:11)
I/flutter ( 8045): #6 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3674:5)
I/flutter ( 8045): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 8045): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 8045): #9 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:909:16)
I/flutter ( 8045): #10 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:880:5)
I/flutter ( 8045): #11 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:826:17)
I/flutter ( 8045): #12 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 8045): #13 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:825:13)
I/flutter ( 8045): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:712:7)
I/flutter ( 8045): #15 runApp (package:flutter/src/widgets/binding.dart:756:7)
I/flutter ( 8045): #16 main (package:test_shared_preferenced/main.dart:5:16)
I/flutter ( 8045): #17 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
I/flutter ( 8045): #18 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 8045): ════════════════════════════════════════════════════════════════════════════════════════════════════


so what should i do in this situation , please help










share|improve this question













In my simple project , there is a button to change theme form light to dark and vice versa as below :



import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return new MyAppState();
}
}

class MyAppState extends State<MyApp> {
Future<bool> load(String key, bool defaultValue) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool state = prefs.getBool(key) ?? defaultValue;
return state;
}

Future<void> save(String key, bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool(key, value);
}

bool state;

@override
Widget build(BuildContext context) {
load("themeKey", false).then((bool value) {
state = value;
});
debugPrint(
"..............state is : " + state.toString() + "...............");
return MaterialApp(
theme: state ? ThemeData.light() : ThemeData.dark(),
home: Scaffold(
body: Container(
child: RaisedButton(
child: Text("change"),
onPressed: () {
setState(() {
state = !state;
});
save("themeKey", state);
},
),
),
),
);
}
}


I use shared_preferences plugin to save the current theme state for the next app launch , but there is a problem it seems that app loads before shared preference loads so i am getting this exception:



Launching libmain.dart on C1905 in debug mode...
Built buildappoutputsapkdebugapp-debug.apk.
I/flutter ( 8045): ..............state is : null...............
I/flutter ( 8045): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 8045): The following assertion was thrown building MyApp(dirty, state: MyAppState#6774f):
I/flutter ( 8045): Failed assertion: boolean expression must not be null
I/flutter ( 8045): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 8045): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 8045): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 8045): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 8045): When the exception was thrown, this was the stack:
I/flutter ( 8045): #0 MyAppState.build (package:test_shared_preferenced/main.dart:36:14)
I/flutter ( 8045): #1 StatefulElement.build (package:flutter/src/widgets/framework.dart:3787:27)
I/flutter ( 8045): #2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3699:15)
I/flutter ( 8045): #3 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 8045): #4 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3679:5)
I/flutter ( 8045): #5 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3826:11)
I/flutter ( 8045): #6 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3674:5)
I/flutter ( 8045): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 8045): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 8045): #9 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:909:16)
I/flutter ( 8045): #10 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:880:5)
I/flutter ( 8045): #11 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:826:17)
I/flutter ( 8045): #12 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 8045): #13 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:825:13)
I/flutter ( 8045): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:712:7)
I/flutter ( 8045): #15 runApp (package:flutter/src/widgets/binding.dart:756:7)
I/flutter ( 8045): #16 main (package:test_shared_preferenced/main.dart:5:16)
I/flutter ( 8045): #17 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
I/flutter ( 8045): #18 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 8045): ════════════════════════════════════════════════════════════════════════════════════════════════════


so what should i do in this situation , please help







dart flutter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 17:26









Hasan

275




275












  • state is return null, change condition to: Dart return MaterialApp( theme: state == null ? ThemeData.light() : ThemeData.dark(), ...)
    – EdHuamani
    Nov 19 at 17:45










  • @EdgarHuamani What you are trying to suggest will prevent the null value error, but not properly take into account the user's theme preference. The vast majority of the time state will not have been set before this code is executed. This means it will default to the light theme regardless of how the user preference is set. It would be better to set the theme once load future completes and the theme preference is known.
    – Stephen
    Nov 19 at 17:52










  • I want to make execution wait for load method to finish , how can i achieve this ,please
    – Hasan
    Nov 19 at 17:58










  • @Stephen I have only answered how to solve the error that the log shows. You're right, the ThemeData.light () will always be selected, @Hasan you can choose to use FutureBuilder I think you already have a response below.
    – EdHuamani
    Nov 19 at 18:23










  • ok , thanks a lot
    – Hasan
    Nov 19 at 19:05


















  • state is return null, change condition to: Dart return MaterialApp( theme: state == null ? ThemeData.light() : ThemeData.dark(), ...)
    – EdHuamani
    Nov 19 at 17:45










  • @EdgarHuamani What you are trying to suggest will prevent the null value error, but not properly take into account the user's theme preference. The vast majority of the time state will not have been set before this code is executed. This means it will default to the light theme regardless of how the user preference is set. It would be better to set the theme once load future completes and the theme preference is known.
    – Stephen
    Nov 19 at 17:52










  • I want to make execution wait for load method to finish , how can i achieve this ,please
    – Hasan
    Nov 19 at 17:58










  • @Stephen I have only answered how to solve the error that the log shows. You're right, the ThemeData.light () will always be selected, @Hasan you can choose to use FutureBuilder I think you already have a response below.
    – EdHuamani
    Nov 19 at 18:23










  • ok , thanks a lot
    – Hasan
    Nov 19 at 19:05
















state is return null, change condition to: Dart return MaterialApp( theme: state == null ? ThemeData.light() : ThemeData.dark(), ...)
– EdHuamani
Nov 19 at 17:45




state is return null, change condition to: Dart return MaterialApp( theme: state == null ? ThemeData.light() : ThemeData.dark(), ...)
– EdHuamani
Nov 19 at 17:45












@EdgarHuamani What you are trying to suggest will prevent the null value error, but not properly take into account the user's theme preference. The vast majority of the time state will not have been set before this code is executed. This means it will default to the light theme regardless of how the user preference is set. It would be better to set the theme once load future completes and the theme preference is known.
– Stephen
Nov 19 at 17:52




@EdgarHuamani What you are trying to suggest will prevent the null value error, but not properly take into account the user's theme preference. The vast majority of the time state will not have been set before this code is executed. This means it will default to the light theme regardless of how the user preference is set. It would be better to set the theme once load future completes and the theme preference is known.
– Stephen
Nov 19 at 17:52












I want to make execution wait for load method to finish , how can i achieve this ,please
– Hasan
Nov 19 at 17:58




I want to make execution wait for load method to finish , how can i achieve this ,please
– Hasan
Nov 19 at 17:58












@Stephen I have only answered how to solve the error that the log shows. You're right, the ThemeData.light () will always be selected, @Hasan you can choose to use FutureBuilder I think you already have a response below.
– EdHuamani
Nov 19 at 18:23




@Stephen I have only answered how to solve the error that the log shows. You're right, the ThemeData.light () will always be selected, @Hasan you can choose to use FutureBuilder I think you already have a response below.
– EdHuamani
Nov 19 at 18:23












ok , thanks a lot
– Hasan
Nov 19 at 19:05




ok , thanks a lot
– Hasan
Nov 19 at 19:05












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The load method is returns a Future that will run asynchronously from the rest of your code. When execution reaches debugPrint or the subsequent return statement, you cannot be guaranteed that the then block has completed its execution. The error you are receiving confirms this. The state boolean has not been set to a value yet - it is still null.



Using a FutureBuilder can help you handle cases where you are waiting on a Future to build your widget tree.



https://www.dartlang.org/tutorials/language/futures is worthwhile reading.






share|improve this answer





















  • It works perfectly with FutureBuilder , thank you very much
    – Hasan
    Nov 19 at 19:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379786%2fhow-to-change-app-theme-in-runtime-in-flutter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The load method is returns a Future that will run asynchronously from the rest of your code. When execution reaches debugPrint or the subsequent return statement, you cannot be guaranteed that the then block has completed its execution. The error you are receiving confirms this. The state boolean has not been set to a value yet - it is still null.



Using a FutureBuilder can help you handle cases where you are waiting on a Future to build your widget tree.



https://www.dartlang.org/tutorials/language/futures is worthwhile reading.






share|improve this answer





















  • It works perfectly with FutureBuilder , thank you very much
    – Hasan
    Nov 19 at 19:03















up vote
1
down vote



accepted










The load method is returns a Future that will run asynchronously from the rest of your code. When execution reaches debugPrint or the subsequent return statement, you cannot be guaranteed that the then block has completed its execution. The error you are receiving confirms this. The state boolean has not been set to a value yet - it is still null.



Using a FutureBuilder can help you handle cases where you are waiting on a Future to build your widget tree.



https://www.dartlang.org/tutorials/language/futures is worthwhile reading.






share|improve this answer





















  • It works perfectly with FutureBuilder , thank you very much
    – Hasan
    Nov 19 at 19:03













up vote
1
down vote



accepted







up vote
1
down vote



accepted






The load method is returns a Future that will run asynchronously from the rest of your code. When execution reaches debugPrint or the subsequent return statement, you cannot be guaranteed that the then block has completed its execution. The error you are receiving confirms this. The state boolean has not been set to a value yet - it is still null.



Using a FutureBuilder can help you handle cases where you are waiting on a Future to build your widget tree.



https://www.dartlang.org/tutorials/language/futures is worthwhile reading.






share|improve this answer












The load method is returns a Future that will run asynchronously from the rest of your code. When execution reaches debugPrint or the subsequent return statement, you cannot be guaranteed that the then block has completed its execution. The error you are receiving confirms this. The state boolean has not been set to a value yet - it is still null.



Using a FutureBuilder can help you handle cases where you are waiting on a Future to build your widget tree.



https://www.dartlang.org/tutorials/language/futures is worthwhile reading.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:45









Stephen

1,762621




1,762621












  • It works perfectly with FutureBuilder , thank you very much
    – Hasan
    Nov 19 at 19:03


















  • It works perfectly with FutureBuilder , thank you very much
    – Hasan
    Nov 19 at 19:03
















It works perfectly with FutureBuilder , thank you very much
– Hasan
Nov 19 at 19:03




It works perfectly with FutureBuilder , thank you very much
– Hasan
Nov 19 at 19:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379786%2fhow-to-change-app-theme-in-runtime-in-flutter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin