0

我为地理定位器创建了单独的文件,称为帮助方法和地名和地址属性的地址类,并将它们包含在搜索页面中这是给定的错误

======== 小部件库捕获的异常====================================== ================== 在 null 上调用了 getter 'placename'。接收者:空尝试呼叫:地名

这是搜索页面文件


    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    import 'package:reviver_app/dataproviders/appdata.dart';
    import 'package:reviver_app/globalvariables.dart';
    import 'package:reviver_app/helpers/requesthelper.dart';
    class SearchPage extends StatefulWidget {
      @override
      _SearchPageState createState() => _SearchPageState();
    }
    
    
    
    
    class _SearchPageState extends State<SearchPage> {
      var pickupController = TextEditingController();
      var destinationController = TextEditingController();
      var focusDestination = FocusNode();
      bool focused = false;
      void setFocus() {
        if (!focused) {
          FocusScope.of(context).requestFocus(focusDestination);
          focused = true;
        }
      }
      void searchPlace(String placeName) async {
        if (placeName.length >1){
          String url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$placeName+Amphitheatre&key=$mapKey&sessiontoken=123456789';
          var response = await RequestHelper.getRequest(url);
          if (response == 'failed'){
            return;
          }
          print(response);
    
        }
    
      }
    
    
      @override
      Widget build(BuildContext context) {
        setFocus();
       String address = Provider.of<AppData>(context).pickupaddress.placename ?? ""; this is the error which is null
       pickupController.text = address;
    
        return Scaffold(
          body: Column(
            children: <Widget>[
              Container(
                height: 210,
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.redAccent,
                      blurRadius: 5.0,
                      spreadRadius: 0.5,
                      offset: Offset(
                        0.7,
                        0.7,
                      ),
                    ),
                  ]
                ),
                child:  Padding(
                  padding:  EdgeInsets.only(left: 24, top: 48, right:24, bottom: 20 ),
                  child: Column(
                    children: <Widget> [
                      SizedBox(height: 5),
                      Stack(
                        children: <Widget>[
                          GestureDetector(
                            onTap: (){
                              Navigator.pop(context);
                                      },
                              child: Icon(
                                Icons.arrow_back, color: Colors.redAccent,)
                          ),
                          Center(
                            child: Text('Set Booking', style:
                            TextStyle(fontSize: 20, fontFamily: 'Brand-Bold'),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 18,),
                      Row(
                        children: <Widget> [
                          Image.asset('images/pickicon.png', height:16, width: 16 ,),
    
                          SizedBox(width: 18,),
                          Expanded(
                            child: Container(
                              decoration: BoxDecoration(
                                color: Colors.redAccent,
                                borderRadius: BorderRadius.circular(4),
                              ),
                              child: Padding(
                                padding:  EdgeInsets.all(2.0),
                                child: TextField(
                                 controller: pickupController,
    
                                  decoration: InputDecoration(
                                    hintText: 'Your Location',
                                    fillColor: Colors.redAccent,
                                    filled: true,
                                    border: InputBorder.none,
                                    isDense: true,
                                    contentPadding: EdgeInsets.only(left: 10, top: 0, bottom: 0)
                                  ),
    
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 10,),
    
                      Row(
                        children: <Widget> [
                          Image.asset('images/desticon.png', height:16, width: 16 ,),
    
                          SizedBox(width: 18,),
                          Expanded(
                            child: Container(
                              decoration: BoxDecoration(
                                color: Colors.redAccent,
                                borderRadius: BorderRadius.circular(4),
                              ),
                              child: Padding(
                                padding:  EdgeInsets.all(2.0),
                                child: TextField(
                                  //destination which will be removed
                                  onChanged: (value){
                                    searchPlace(value);
                                  },
                                 focusNode: focusDestination ,
                                  controller: destinationController,
    
                                  decoration: InputDecoration(
                                      hintText: 'this will be removed ',
                                      fillColor: Colors.redAccent,
                                      filled: true,
                                      border: InputBorder.none,
                                      isDense: true,
                                      contentPadding: EdgeInsets.only(left: 10, top: 0, bottom: 0)
                                  ),
    
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
    
    
                    ],
                  ),
                ),
              )
    
            ],
          ),
        );
      }
    }
    ```
this is where I included the geolocotor URL


import 'package:connectivity/connectivity.dart';
import 'package:geolocator/geolocator.dart';
import 'package:reviver_app/datamodels/address.dart';
import 'package:reviver_app/dataproviders/appdata.dart';
import 'package:reviver_app/globalvariables.dart';
import 'package:reviver_app/helpers/requesthelper.dart';
import 'package:provider/provider.dart';


class HelperMethods {
  static Future<String> findCordinateAddress(Position position, context) async {
    String placeaddress = '';
    var connectivityResult = await Connectivity().checkConnectivity();
    if (connectivityResult != ConnectivityResult.mobile &&
        connectivityResult != ConnectivityResult.wifi) {
      return placeaddress;
    }
    String url =
        'https://maps.googleapis.com/maps/api/geocode/json?latlng=${position.latitude},${position.longitude}&key=$mapKey';


    var response = await RequestHelper.getRequest(url);
    if (response == 'failed') {
      placeaddress = response['results'][0]['formatted_address'];

      Address pickupaddress = new Address();
      pickupaddress.longitude = position.longitude;
      pickupaddress.latitude = position.latitude;
      pickupaddress.placename = placeaddress;

      Provider.of<AppData>(context, listen: false)
          .updatePickupAddress(pickupaddress);
    }
    return placeaddress;
  }
}
```

这是应用程序数据文件


    
    import 'package:flutter/cupertino.dart';
    import 'package:reviver_app/datamodels/address.dart';
    
    class AppData extends ChangeNotifier{
      Address pickupaddress;
       void updatePickupAddress(Address pickup){
         pickupaddress = pickup;
         notifyListeners();
       }
    
    
    
    }

这是我添加地名的地址类

  
    class Address {
      String placename;
      double latitude;
      double longitude;
      String placeId;
      String placeformattedaddress;
    
      Address({
        this.placeId,
        this.latitude,
        this.placename,
        this.longitude,
        this.placeformattedaddress,
    
    });
    }

4

0 回答 0