0

I am getting this error : Exception caught by widgets library LateInitializationError: Field 'count' has not been initialized. The relevant error-causing widget was Consumer lib\…\views\base_view.dart:31

How can I fix this error? Thank you ?

import 'package:provider/provider.dart';

import '../../locator.dart';

class BaseView<T extends ChangeNotifier> extends StatefulWidget {
  final Widget Function(BuildContext context, T value, Widget? child) builder;
  final Function(T)? onModelReady;

  BaseView({required this.builder, required this.onModelReady});

  @override
  _BaseViewState<T> createState() => _BaseViewState<T>();
}

class _BaseViewState<T extends ChangeNotifier> extends State<BaseView<T>> {
  T model = locator<T>();

  @override
  void initState() {
    if (widget.onModelReady != null) {
      widget.onModelReady!(model);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<T>(
      create: (context) => model,
      child: Consumer<T>(builder: widget.builder),
    );
  }
}```
1
  • 2
    Where is the count variable declared? Mar 27, 2022 at 7:44

1 Answer 1

0

This error message says that you have declared the "count" variable with the late modifier, which means that you promised that it would not be null, but you got an error, which means you did not keep your promise. To fix this, you should rethink your code or give the count variable a default value. Initialization example:

static int count = 5;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.