Как использовать var в параметре? - PullRequest
0 голосов
/ 04 апреля 2020

как использовать var _latitude и var _longitude как параметр в моей функции addOrderRequest()? Обе переменные должны быть в строке,

весь код ниже, спасибо за любую помощь:)

_getCurrentLocation() async {
    final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    var _latitude = position.latitude;
    var _longitude = position.longitude;
  }

  @override
  Widget build(BuildContext context){
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
      body: SingleChildScrollView(
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              //
            ],
          ),
        ),
      ),
      bottomSheet: Container(
        width: width,
        height: 50.0,
        child: new RaisedButton(
            color: Color.fromRGBO(9, 133, 46, 100),
            onPressed: () async {
                addOrderRequest(titleController.text, zipCodeController.text, cityController.text, streetController.text, phoneNumberController.text, descriptionController.text, _selectedCategory);
                Navigator.push(context,
                  MaterialPageRoute(builder: (context) => OrdersScreen()));

                },

моя функция:

  addOrderRequest(String title, zipCode, city, street, phoneNumber, description, category, latitude, longitude) async{
    Map data = {
      "title":title,
      "zipCode":zipCode,
      "city":city,
      "street":street,
      "phoneNumber":phoneNumber,
      "description":description,
      "category":category,
      "latitude":latitude,
      "longitude":longitude
    };

    var jsonResponse;
    var url = 'http://10.0.2.2:80/order';
    var response = await http.post(url, body:data);

    if(response.statusCode == 200){
      jsonResponse = json.decode(response.body);
      print(jsonResponse);      
    }else{
      throw new Exception("Not send data");
    }
  }

1 Ответ

0 голосов
/ 04 апреля 2020

Просто преобразуйте переменные в строку

addOrderRequest(titleController.text, zipCodeController.text, cityController.text, streetController.text, phoneNumberController.text, descriptionController.text, _selectedCategory, _latitude.toString(), _longitude.toString());

РЕДАКТИРОВАТЬ Объявите переменные вне функции _getCurrentLocation внутри класса.

Как это

class YourWidget extends StatelessWidget{
    var _latitude;
    var _longitude;

    _getCurrentLocation() async {
    final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    _latitude = position.latitude;
    _longitude = position.longitude;
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...