Skip to content

Instantly share code, notes, and snippets.

@MwBakker
Last active February 5, 2024 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MwBakker/0da089a368cfaf16191c285eb3e94a58 to your computer and use it in GitHub Desktop.
Save MwBakker/0da089a368cfaf16191c285eb3e94a58 to your computer and use it in GitHub Desktop.
initial widget causing error setting providers
class AppInitializationWidget extends ConsumerWidget {
const AppInitializationWidget({super.key, required this.onLoaded});
final WidgetBuilder onLoaded;
@override
Widget build(BuildContext ctx, WidgetRef ref) => CustomFutureBuilder(
futures: [
Firebase.initializeApp(),
DataController.vehicleDB.getVehicleSpecification('type'),
DataController.vehicleDB.getVehicleSpecification('class'),
DataController.vehicleDB.getVehicleSpecification('region'),
DataController.vehicleDB.getVehicleSpecification('transmission'),
DataController.companyDB.getCompanySpecialties(),
rootBundle.loadString('assets/map_style.txt'),
SharedPreferences.getInstance(),
],
loading: _loading(),
onDone: (st) {
_setProviderData(ref, st.data);
return onLoaded(ctx);
}
);
void _setProviderData(WidgetRef ref, dynamic data) {
ref.read(vehicleSpecsProvider.notifier).set({
VehicleType: data[1],
VehicleClass: data[2],
VehicleRegion: data[3],
VehicleTransmission: data[4],
});
ref.read(companySpecialtiesProvider.notifier).set(data[5]);
ref.read(mapStyleProvider.notifier).set(data[6]);
DataController.prefs = data[7];
}
}
class CompanySpecialtiesNotifier extends Notifier<List<CompanySpecialty>> {
@override
List<CompanySpecialty> build() => [];
void set(List<CompanySpecialty> specialties) => state = specialties;
}
final companySpecialtiesProvider =
NotifierProvider<CompanySpecialtiesNotifier, List<CompanySpecialty>>(
() => CompanySpecialtiesNotifier(),
);
class CustomFutureBuilder extends StatelessWidget {
final Future? future;
final List<Future>? futures;
final Widget? loading;
final Function(dynamic st) onDone;
final Widget? error;
const CustomFutureBuilder({
this.futures,
this.future,
this.loading,
required this.onDone,
this.error,
});
@override
Widget build(BuildContext ctx) => FutureBuilder(
future: (futures == null) ? future! : Future.wait(futures!),
builder: (ctx, st) {
switch (st.connectionState) {
case ConnectionState.waiting:
return (loading == null)
? Center(child: SpinningLoading())
: loading!;
default:
if (!st.hasError) {
return onDone(st);
} else {
return (error == null)
? CustomError(body: '${st.error} \n \n ${st.stackTrace}')
: error!;
}
}
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment