SolidProvider - async create function - adjustment possible? #54
-
I would like to create a PreferencesController that needs to call some asynchronous code in order to be fully initialized. class ExampleWidget extends StatelessWidget {
// omitted content
@override
Widget build(BuildContext context) {
return Solid(
providers: [
SolidProvider<PreferencesController>(
create: () async => await PreferencesController.init()) // unfortunately this is not valid
],
// omitted content
);
}
} class PreferencesController {
// private constructor => only internal use
PreferencesController._init();
// init() functions needs to be used to create a PreferencesController
static Future<PreferencesController> init() async {
var preferenceController = PreferencesController._init();
await preferenceController._initAsyncStuff();
// return a fully initialized controller
return preferenceController;
}
Future<void> _initAsyncStuff() async {
// do async stuff here
}
// omitted content
} I have thought of adjusting the create function of a SolidProvider more open for asyncronous functions. Since I don't yet understand all the relationships as well as possible implications in this library, the idea may also be completely off-base. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is not currently feasible and I think it will be pretty hard to implement. class _ExampleWidgetState extends State<ExampleWidget> {
late final resource = createResource(fetcher: PreferencesController.init);
@override
void dispose() {
resource.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ResourceBuilder(
resource: resource,
builder: (context, resourceState) {
return resourceState.on(
ready: (preferencesController) {
return Solid(
providers: [
SolidProvider<PreferencesController>(
create: () => preferencesController,
),
],
child: const Placeholder(),
);
},
loading: () => const CircularProgressIndicator(),
error: (error, stackTrace) => Text('$error'),
);
},
);
}
} or by using a FutureBuilder: class _ExampleWidgetState extends State<ExampleWidget> {
final future = PreferencesController.init();
@override
Widget build(BuildContext context) {
return FutureBuilder<PreferencesController>(
future: future,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) return const SizedBox();
return Solid(
providers: [
SolidProvider<PreferencesController>(
create: () => snapshot.data,
),
],
child: const Placeholder(),
);
},
);
}
} or at app startup: Future<void> main() async {
final preferencesController = await PreferencesController.init();
runApp(
Solid(
providers: [
SolidProvider<PreferencesController>(
create: () => preferencesController,
),
],
child: const MyApp(),
),
);
} Let me know it this sounds reasonable for you. |
Beta Was this translation helpful? Give feedback.
This is not currently feasible and I think it will be pretty hard to implement.
You can await the initialisation before creating the
SolidProvider
, for example: