0

I have a BaseView that contains ChangeNotifieProvider and Consumer which will be common to use anywhere. This Widget also receives Generic types of ViewModel. It has onModelReady that to be called inside init state. Also using get_it for Dependency injection.

Issue: Whenever the user inserts a new entry and calls fetch data, data gets loaded but UI still remains as it is.

If I remove the ChangeNotifierProvider and use only Consumer then it's re-rendering UI in a proper way. But I cannot pass the onModelReady function that is to be called in initState()

:::::::::::CODE:::::::::::::::::::::::::

base_view.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:businesshub/injections/injection_container.dart';
import 'package:businesshub/features/views/viewmodels/base_model.dart';

class BaseView<T extends BaseModel> extends StatefulWidget {
  const BaseView({
    Key? key,
    this.onModelReady,
    required this.builder,
  }) : super(key: key);
  final Function(T)? onModelReady;
  final Widget Function(BuildContext context, T model, Widget? child) builder;

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

class _BaseViewState<T extends BaseModel> extends State<BaseView<T>> {
  T model = locator<T>();
  @override
  void initState() {
    super.initState();

    if (widget.onModelReady != null) {
      widget.onModelReady!(model);
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<T>(
      create: (context) => model,
      child: Consumer<T>(
        builder: widget.builder,
      ),
    );
  }
}



USING::::::::::::::::HERE::::::::::::::::::::::

class RecentBillBuilder extends StatelessWidget {
  const RecentBillBuilder({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseView<SalesBillViewModel>(
      onModelReady: (model) {
        model.fetchAndSetSalesBills(currentUser!.uid);
        model.searchController.clear();
      },
      builder: (ctx, model, _) {
        if (model.state == ViewState.busy) {
          return Center(
            child: CircularProgressIndicator.adaptive(),
          );
        }

        return model.bills.fold(
          (l) => ResourceNotFound(title: l.message!),
          (r) => (r.isEmpty)
              ? ResourceNotFound(title: "Sales Bills not created yet!")
              : ListView.builder(
                  itemCount: min(r.length, 7),
                  shrinkWrap: true,
                  reverse: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (ctx, index) {
                    return RecentBillsCard(bill: r[index]);
                  },
                ),
        );
      },
    );
  }
}

0 Answers0