在 null 上调用了 getter 'currentState'

时间:2021-01-08 13:54:50

标签: flutter dart

我创建了一个页面来添加地址,当我按下“添加地址”按钮时,它应该打开 DeliveryAddressBottomSheetWidget 但它显示错误

==╡ 异常被捕获 I/flutter (13370):处理手势时抛出以下 NoSuchMethodError: I/flutter (13370):在 null 上调用了 getter 'currentState'。 I/flutter (13370):接收器:空 I/flutter (13370):尝试调用:currentState

  class Location2Widget extends StatefulWidget {
  final GlobalKey<ScaffoldState> parentScaffoldKey;

  Location2Widget({Key key, this.parentScaffoldKey}) : super(key: key);
  


  @override
  _Location2WidgetState createState() => _Location2WidgetState();
}

class _Location2WidgetState extends StateMVC<Location2Widget> {
  HomeController _con;
  _Location2WidgetState() : super(HomeController()) {
    _con = controller;
  }
  

  

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      
      body: RefreshIndicator(
        onRefresh: _con.refreshHome,
        child: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 0, vertical: 10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: [
                 Padding(
                    padding: const EdgeInsets.only(top: 15, left: 20, right: 20, bottom: 0),
                     child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            
                            InkWell(
                              onTap: () {
                                //if (currentUser.value.apiToken == null) {
                                  //_con.requestForCurrentLocation(context);
                                //} else {
                                  var bottomSheetController = widget.parentScaffoldKey.currentState.showBottomSheet(
                                    (context) => DeliveryAddressBottomSheetWidget(scaffoldKey: widget.parentScaffoldKey),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: new BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
                                    ),
                                  );
                                  bottomSheetController.closed.then((Navigator)  {
                                    _con.refreshHome();
                                    
                                  });
                                },
                              
                              child: Container(
                                padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 10),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(5)),
                                  color: settingsRepo.deliveryAddress.value?.address == null
                                      ? Theme.of(context).accentColor
                                      : Theme.of(context).accentColor,
                                ),
                                child: Text(
                                  S.of(context).change_delivery,
                                  style: TextStyle(
                                      color:
                                          settingsRepo.deliveryAddress.value?.address == null ? Theme.of(context).primaryColor : Theme.of(context).primaryColor),
                                ),
                              ),
                            ),
                            
                            
                            
                          ],
                        ),
                        if (settingsRepo.deliveryAddress.value?.address != null)
                          Padding(
                            padding: const EdgeInsets.only(top: 12),
                            child: Text(
                              S.of(context).near_to + " " + (settingsRepo.deliveryAddress.value?.address),
                              style: Theme.of(context).textTheme.caption,
                            ),
                            
                          ),
                          
                          
                      ],
                    ),
                  ),
            ],
                
                
              
            ),
          ),
        ),
      );
      
    
 }
            
              
}

DeliveryAddressBottomSheetWidget 代码

class DeliveryAddressBottomSheetWidget extends StatefulWidget {
  final GlobalKey<ScaffoldState> scaffoldKey;

  DeliveryAddressBottomSheetWidget({Key key, this.scaffoldKey}) : super(key: key);

  @override
  _DeliveryAddressBottomSheetWidgetState createState() => _DeliveryAddressBottomSheetWidgetState();
}

class _DeliveryAddressBottomSheetWidgetState extends StateMVC<DeliveryAddressBottomSheetWidget> {
  DeliveryAddressesController _con;

  _DeliveryAddressBottomSheetWidgetState() : super(DeliveryAddressesController()) {
    _con = controller;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 350,
      decoration: BoxDecoration(
        color: Theme.of(context).primaryColor,
        borderRadius: BorderRadius.only(topRight: Radius.circular(20), topLeft: Radius.circular(20)),
        boxShadow: [
          BoxShadow(color: Theme.of(context).focusColor.withOpacity(0.4), blurRadius: 30, offset: Offset(0, -30)),
        ],
      ),
      child: Stack(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 30),
            child: ListView(
              padding: EdgeInsets.only(top: 20, bottom: 15, left: 20, right: 20),
              children: <Widget>[
                InkWell(
                  onTap: () async {
                    LocationResult result = await showLocationPicker(
                      context,
                      setting.value.googleMapsKey,
                      initialCenter: LatLng(deliveryAddress.value?.latitude ?? 0, deliveryAddress.value?.longitude ?? 0),
                      automaticallyAnimateToCurrentLocation: true,
                      //mapStylePath: 'assets/mapStyle.json',
                      myLocationButtonEnabled: true,
                      resultCardAlignment: Alignment.bottomCenter,
                    );
                    _con.addAddress(new Address.fromJSON({
                      'address': result.address,
                      'latitude': result.latLng.latitude,
                      'longitude': result.latLng.longitude,
                    }));
                    print("result = $result");
                     //Navigator.of(widget.scaffoldKey.currentContext).pop();
                  },
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        height: 36,
                        width: 36,
                        decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(5)), color: Theme.of(context).accentColor),
                        child: Icon(
                          Icons.add_circle_outline,
                          color: Theme.of(context).primaryColor,
                          size: 22,
                        ),
                      ),
                      SizedBox(width: 15),
                      Flexible(
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(
                                    S.of(context).add_new_delivery_address,
                                    overflow: TextOverflow.ellipsis,
                                    maxLines: 2,
                                    style: Theme.of(context).textTheme.bodyText2,
                                  ),
                                ],
                              ),
                            ),
                            SizedBox(width: 8),
                            Icon(
                              Icons.keyboard_arrow_right,
                              color: Theme.of(context).accentColor,
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                
                ListView.separated(
                  padding: EdgeInsets.symmetric(vertical: 25),
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  primary: false,
                  itemCount: _con.addresses.length,
                  separatorBuilder: (context, index) {
                    return SizedBox(height: 25);
                  },
                  itemBuilder: (context, index) {
//                return DeliveryAddressesItemWidget(
//                  address: _con.addresses.elementAt(index),
//                  onPressed: (Address _address) {
//                    _con.chooseDeliveryAddress(_address);
//                  },
//                  onLongPress: (Address _address) {
//                    DeliveryAddressDialog(
//                      context: context,
//                      address: _address,
//                      onChanged: (Address _address) {
//                        _con.updateAddress(_address);
//                      },
//                    );
//                  },
//                  onDismissed: (Address _address) {
//                    _con.removeDeliveryAddress(_address);
//                  },
//                );
                    return InkWell(
                      onTap: () {
                        _con.changeDeliveryAddress(_con.addresses.elementAt(index)).then((value) {
                          Navigator.of(context).pushNamed('/Pages');
                        });
                      },
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Container(
                            height: 36,
                            width: 36,
                            decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(5)), color: Theme.of(context).focusColor),
                            child: Icon(
                              Icons.place,
                              color: Theme.of(context).primaryColor,
                              size: 22,
                            ),
                          ),
                          SizedBox(width: 15),
                          Flexible(
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Text(
                                        _con.addresses.elementAt(index).address,
                                        overflow: TextOverflow.ellipsis,
                                        maxLines: 3,
                                        style: Theme.of(context).textTheme.bodyText2,
                                      ),
                                    ],
                                  ),
                                ),
                                SizedBox(width: 8),
                                Icon(
                                  Icons.keyboard_arrow_right,
                                  color: Theme.of(context).focusColor,
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                    );
                  },
                ),
              ],
            ),
          ),
          Container(
            height: 30,
            width: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 13, horizontal: config.App(context).appWidth(42)),
            decoration: BoxDecoration(
              color: Theme.of(context).focusColor.withOpacity(0.05),
              borderRadius: BorderRadius.only(topRight: Radius.circular(20), topLeft: Radius.circular(20)),
            ),
            child: Container(
              width: 30,
              decoration: BoxDecoration(
                color: Theme.of(context).focusColor.withOpacity(0.8),
                borderRadius: BorderRadius.circular(3),
              ),
              //child: SizedBox(height: 1,),
            ),
          ),
        ],
      ),
    );
  }
}

错误详细信息

I/flutter (13370): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (13370): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter (13370): The getter 'currentState' was called on null.
I/flutter (13370): Receiver: null
I/flutter (13370): Tried calling: currentState
I/flutter (13370): 
I/flutter (13370): When the exception was thrown, this was the stack:
I/flutter (13370): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter (13370): #1      _Location2WidgetState.build.<anonymous closure> (package:food_delivery_app/src/pages/location2.dart:62:88)
I/flutter (13370): #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
I/flutter (13370): #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
I/flutter (13370): #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
I/flutter (13370): #5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
I/flutter (13370): #6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
I/flutter (13370): #7      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
I/flutter (13370): #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:476:9)
I/flutter (13370): #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:77:12)
I/flutter (13370): #10     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:122:9)
I/flutter (13370): #11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
I/flutter (13370): #12     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:120:18)
I/flutter (13370): #13     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:106:7)
I/flutter (13370): #14     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:358:19)
I/flutter (13370): #15     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:338:22)
I/flutter (13370): #16     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:267:11)
I/flutter (13370): #17     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:295:7)
I/flutter (13370): #18     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:240:7)
I/flutter (13370): #19     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:213:7)
I/flutter (13370): #23     _invoke1 (dart:ui/hooks.dart:265:10)
I/flutter (13370): #24     _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)
I/flutter (13370): (elided 3 frames from dart:async)
I/flutter (13370): 
I/flutter (13370): Handler: "onTap"
I/flutter (13370): Recognizer:
I/flutter (13370):   TapGestureRecognizer#113cd
I/flutter (13370): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (13370): Another exception was thrown: NoSuchMethodError: The getter 'currentState' was called on null.

如果您需要更多代码,请告诉我

0 个答案:

没有答案