Unable to render widget dynamically in Flutter using Provider?

I am trying to display widget(s) dynamically based on the empty/non-empty state of a TextField widget. Below is the code I’ve written. But the code does not dynamically create the widget even when I type in text in the TextField of Tax Percentage. This is unexpected. Here, I am also not sure if we can late assign the Provider the way I have done in the code.

What I’ve tried. I’ve tried declaring the boolean variable isEmptyTaxPercentage in the Provider class as static and set using static access method in the onChanged of the TaxPercentage TextField. But, as no notifyListeners() function was called, perhaps, this also did not work as expected: the intended widget didn’t show up dynamically.

Update 01: Fixed the code below. Now it works as intended. The only question remaining is declaration of the provider as late MyTaxProvider myTaxProvider in the State class is valid and good practice or not?

The code:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<MyTaxProvider>(
          create: (context) => MyTaxProvider(),
        ),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Provider Demo'),
    );
  }
}

class MyTaxProvider with ChangeNotifier {
  bool isEmptyTaxPercentage = true;

  void setTaxPercentageEmptynessState(bool value) {
    isEmptyTaxPercentage = value;
    notifyListeners();
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late final TextEditingController costController;
  late final TextEditingController taxPercentageController;
  late final TextEditingController taxAmountController;

  late MyTaxProvider myTaxProvider;

  @override
  void initState() {
    super.initState();

    costController = TextEditingController();
    taxPercentageController = TextEditingController();
    taxAmountController = TextEditingController();

    myTaxProvider = Provider.of<MyTaxProvider>(context, listen: false);
  }

  @override
  void dispose() {
    costController.dispose();
    taxPercentageController.dispose();
    taxAmountController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Row(
              children: [
                const Expanded(
                  child: Text("Cost:"),
                ),
                Expanded(
                  child: TextField(
                    controller: costController,
                  ),
                ),
              ],
            ),
            Row(
              children: [
                const Expanded(
                  child: Text("Tax precentage (%):"),
                ),
                Expanded(
                  child: TextField(
                    controller: taxPercentageController,
                    onChanged: (value) {
                      if (value.isEmpty) {
                        myTaxProvider.setTaxPercentageEmptynessState(true);
                      } else {
                        myTaxProvider.setTaxPercentageEmptynessState(false);
                      }
                    },
                  ),
                ),
              ],
            ),
            Consumer<MyTaxProvider>(
              builder: (context, provider, child) {
                if (provider.isEmptyTaxPercentage) {
                  return Container();
                } else {
                  return Row(
                    children: [
                      const Expanded(
                        child: Text("Tax Amount:"),
                      ),
                      Expanded(
                        child: TextField(
                          controller: taxAmountController,
                        ),
                      ),
                    ],
                  );
                }
              },
            ),
            const Text("Other widget(s)."),
          ],
        ),
      ),
    );
  }
}

Declaring the provider as late is not recommended you should probably instead use it like below inside your build method

final myTaxProvider = Provider.of<MyTaxProvider>(context, listen: false);

That way your code is more clear and avoid potential errors regarding late initialization.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật