How to maintain the current state of global key in flutter

22 Views Asked by At

I'm using this key

final GlobalKey<DropdownSearchState<ProductModel>> _productKey = GlobalKey();

Using this key in the following dropdown_search

Widget loadProducts() {
    return DropdownSearch<ProductModel>(
      key: _productKey,
      asyncItems: (String filter) async {
        var response = await APIServices.getProducts();
        var models = response.data;
        return models ?? [];
      },
      itemAsString: (ProductModel item) =>
          '${item.productId} - ${item.productName!}',
      onChanged: (item) async {
        if (item != null) {
          _productModel = item;
          fetchProductData(_productModel!.productId!);
        }
      },
      selectedItem: _productModel,
    );
  }

And I'm using Layout Builder to make my app responsive on both desktop and mobile. And here is the basic structure of my screen. I'm calling the editRow() from DataCell in datatable and edit button on ListView.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ResponsiveWidget(
        largeScreen: SingleChildScrollView(
          child: Column(
            children: [
              loadProducts(),
              DataTable(columns: [], rows: [])
            ]
          ),
        ),
        smallScreen: Column(
          children: [
            TabBar(
              controller: _tabbarController,
              tabs: [
                Tab(text: 'Order',),
                Tab(text: 'Products'),
                Tab(text: 'Detail')
              ],
            ),
            TabBarView(
              controlerr: _tabbarController,
                children: [
              TabBar1Content(),
              Column(
                children: [
                  TabBar(
                    controller: _tabbarController2,
                    tabs: [
                      Tab(text: 'Product'),
                      Tab(text: 'List')
                    ]
                  ),
                  TabBarView(
                    controller: _tabbarController2
                      children: [
                    Column(
                      children: [
                        loadProducts(),
                        ListView.builder(itemBuilder: (context, index) => ProductCard(onEditClick: (){
                          editRow()
                        }))
                      ],
                    )
                  ])
                ],
              )
              TabBar3Content()
            ])
          ]
        ),
      ),
    );
  }

I'm having this error only in smallScreen while accessing the currentState of _productKey. I'm confused here what I'm doing wrong.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value #0      _AddSaleOrderScreenState.editRow (package:ultra_erp/screens/sale/add_screen/add_sale_order_screen.dart:708:29) #1      _AddSaleOrderScreenState.build.<anonymous closure>.<anonymous closure> (package:ultra_erp/screens/sale/add_screen/add_sale_order_screen.dart:2542:45) #2      CustomSlidableAction._handleTap (package:flutter_slidable/src/actions.dart:116:16) #3      CustomSlidableAction.build.<anonymous closure> (package:flutter_slidable/src/actions.dart:98:28) #4      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1183:21) #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:315:24) #6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:652:11) #7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:309:5)

I tried to debug the problem using print(_productKey.currentState!.mounted); at different stages. But as long as I navigate to list tab and call the editRow function it throws the above null check error.

0

There are 0 best solutions below