' lateerror (LateInitializationEerror: поле' __storage@30190796 ' не был инициализирован.) '. .dart:
Код: Выделить всё
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:hydrated_bloc_debugging/counter/counter_cubit.dart';
import 'package:path_provider/path_provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: HydratedStorageDirectory(
(await getApplicationDocumentsDirectory()).path,
),
);
runApp(
const MyApp(),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => CounterCubit(),
child: MaterialApp(
home:
const MyHomePage(), // Use a named route or a stateless widget here
),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Counter App'),
),
body: Center(
child: BlocBuilder(
builder: (context, state) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'${state.counterValue}',
),
],
);
},
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
FloatingActionButton(
onPressed: () {
context.read().increment();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
FloatingActionButton(
onPressed: () {
context.read().decrement();
},
tooltip: 'Decrement',
child: const Icon(Icons.remove),
),
],
),
);
}
}
Код: Выделить всё
import 'package:equatable/equatable.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
part 'counter_state.dart';
class CounterState extends Equatable {
final int counterValue;
const CounterState({
required this.counterValue,
});
@override
List get props => [counterValue];
factory CounterState.fromMap(Map json) => CounterState(
counterValue: json['counterValue'] as int,
);
Map toMap() {
return {
'counterValue': counterValue,
};
}
}
Код: Выделить всё
part of 'counter_cubit.dart';
class CounterCubit extends Cubit with HydratedMixin {
CounterCubit() : super(CounterState(counterValue: 0));
void increment() => emit(
CounterState(counterValue: state.counterValue + 1),
);
void decrement() => emit(
CounterState(counterValue: state.counterValue - 1),
);
@override
CounterState? fromJson(Map json) {
return CounterState.fromMap(json);
}
@override
Map toJson(CounterState state) => state.toMap();
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... 96-has-not