【问题标题】:The getter 'placeName' was called on null. Receiver: null在 null 上调用了 getter 'placeName'。接收方:空
【发布时间】:2021-09-13 01:39:20
【问题描述】:
 Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building SearchScreen(dirty, dependencies: [_InheritedProviderScope<AppData?>], state: _SearchScreenState#da8a1):
The getter 'placeName' was called on null.
Receiver: null
Tried calling: placeName

相关的导致错误的小部件是:

  SearchScreen file:///C:/Users/luiss/AndroidStudioProjects/motorider/lib/AllScreens/mainscreen.dart:194:88
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      _SearchScreenState.build (package:motorider/AllScreens/searchScreen.dart:19:72)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
...

这里是代码

  import 'package:flutter/material.dart';
  import 'package:motorider/DataHandler/appData.dart';
  import 'package:provider/provider.dart';

class SearchScreen extends StatefulWidget {
@override
_SearchScreenState createState() => _SearchScreenState();
}

class _SearchScreenState extends State<SearchScreen>
{

TextEditingController pickUpTextEditingController = TextEditingController();
TextEditingController dropOffTextEditingController = TextEditingController();

@override
Widget build(BuildContext context)
 {
String placeAddress = Provider.of<AppData>(context).pickUpLocation.placeName ?? "n/a";
pickUpTextEditingController.text = placeAddress;

return Scaffold(
  body: Column(
    children: [
      Container(
        height: 215.0,
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.black,
              blurRadius: 6.0,
              spreadRadius: 0.5,
              offset: Offset(0.7, 0.7),
            ),
          ],
        ),
        child: Padding(
          padding: EdgeInsets.only(left: 25.0, top: 25.0, right: 25.0, bottom: 20.0),
          child: Column(
            children: [
              SizedBox(height: 5.0,),
              Stack(
                children: [
                  GestureDetector(
                    onTap:()
                    {
                      Navigator.pop(context);
                    },
                    child: Icon(
                        Icons.arrow_back
                    ),
                  ),

                  Center(
                    child: Text("Dejar En", style: TextStyle(fontSize: 18.0, fontFamily: "Brand Bold"),),
                  )
                ],
              ),
              SizedBox(height: 16.0),
              Row(
                children: [
                  Image.asset("images/pickicon.png", height: 16.0, width: 16.0,),

                  SizedBox (width:18.0),

                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.grey[400],
                        borderRadius: BorderRadius.circular(5.0),
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(3.0),
                        child: TextField(
                          controller: pickUpTextEditingController,
                          decoration: InputDecoration(
                            hintText: "Recoger En",
                            fillColor: Colors.grey[400],
                            filled: true,
                            border: InputBorder.none,
                            isDense: true,
                            contentPadding: EdgeInsets.only(left: 11.0, top: 8.0, bottom: 8.0),
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),

              SizedBox(height: 10.0),
              Row(
                children: [
                  Image.asset("images/desticon.png", height: 16.0, width: 16.0,),

                  SizedBox (width:18.0),

                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.grey[400],
                        borderRadius: BorderRadius.circular(5.0),
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(3.0),
                        child: TextField(
                          controller: dropOffTextEditingController,
                          decoration: InputDecoration(
                            hintText: "¿A dónde??",
                            fillColor: Colors.grey[400],
                            filled: true,
                            border: InputBorder.none,
                            isDense: true,
                            contentPadding: EdgeInsets.only(left: 11.0, top: 8.0, bottom: 8.0),
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
      ),
    ],
  ),
);
}
}

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    您的pickUpLocation 变量可能在其placeName 上具有null 值。

    【讨论】:

      【解决方案2】:

      飞镖说 Provider.of(context).pickUpLocation 为空。因此,您尝试调用 null.placeName 并发生错误。只需添加“?”在pickUpLocation之后是这样的:

      String placeAddress = Provider.of<AppData>(context).pickUpLocation?.placeName ?? "n/a";
      

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 2018-12-22
        • 2021-09-07
        • 2023-03-20
        • 1970-01-01
        • 2021-09-13
        • 2021-03-06
        • 2021-06-25
        • 1970-01-01
        相关资源
        最近更新 更多