Skip to content

Instantly share code, notes, and snippets.

@bizz84
Created September 11, 2025 09:47
Show Gist options
  • Save bizz84/beb6c50c98385cbcc33a24c4e204d59b to your computer and use it in GitHub Desktop.
Save bizz84/beb6c50c98385cbcc33a24c4e204d59b to your computer and use it in GitHub Desktop.
Sample code for Riverpod issue #4282 (inconsistent provider lifecycle)
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'main.g.dart';
void main() {
runApp(ProviderScope(child: const MainApp()));
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
builder: (context, child) {
return AppStartupWidget(onLoaded: (context) => child!);
},
);
}
}
@riverpod
class AppStartupNotifier extends _$AppStartupNotifier {
@override
Future<void> build() async {
await Future.delayed(Duration(seconds: 2));
await ref.watch(packageInfoProvider.future);
}
}
class AppStartupWidget extends ConsumerWidget {
const AppStartupWidget({super.key, required this.onLoaded});
final WidgetBuilder onLoaded;
@override
Widget build(BuildContext context, WidgetRef ref) {
// 1. eagerly initialize appStartupProvider (and all the providers it depends on)
final appStartupState = ref.watch(appStartupProvider);
return appStartupState.when(
// 2. loading state
loading: () {
return Scaffold(
appBar: AppBar(),
body: Center(child: CircularProgressIndicator()),
);
},
// 3. error state
error: (e, st) {
return Scaffold(
appBar: AppBar(),
body: Center(child: Text('Loading Error: $e')),
);
},
// 5. success - now load the main app
data: (success) {
return onLoaded(context);
},
);
}
}
@Riverpod(keepAlive: true)
Future<PackageInfo> packageInfo(Ref ref) {
return PackageInfo.fromPlatform();
}
class HomeScreen extends ConsumerWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final packageInfo = ref.watch(packageInfoProvider).requireValue;
return Scaffold(
appBar: AppBar(title: Text(packageInfo.appName)),
body: Center(
child: Text('${packageInfo.appName} ${packageInfo.version}'),
),
);
}
}
name: riverpod3_void_async_notifier
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0
environment:
sdk: ^3.9.2
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^3.0.0
riverpod_annotation: ^3.0.0
package_info_plus: ^8.3.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^5.0.0
build_runner: ^2.7.1
riverpod_generator: ^3.0.0
custom_lint: ^0.8.0
riverpod_lint: ^3.0.0
flutter:
uses-material-design: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment