Last active
February 5, 2024 09:54
-
-
Save MwBakker/0da089a368cfaf16191c285eb3e94a58 to your computer and use it in GitHub Desktop.
initial widget causing error setting providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompanySpecialtiesNotifier extends Notifier<List<CompanySpecialty>> { | |
@override | |
List<CompanySpecialty> build() => []; | |
void set(List<CompanySpecialty> specialties) => state = specialties; | |
} | |
final companySpecialtiesProvider = | |
NotifierProvider<CompanySpecialtiesNotifier, List<CompanySpecialty>>( | |
() => CompanySpecialtiesNotifier(), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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