我无法使用解析的整数进行 api 调用



所以我正在使用颤振构建一个应用程序,我是 Flutter 的新手,需要一些帮助。

因此,对于上下文,我的应用程序连接到 REST API,该 API 与虚构电梯公司的 ruby on rails Web 应用程序相关联(用于学习目的(,我试图实现的是使用我的应用程序中的文本字段更改电梯的状态,我几乎明白了, 唯一的问题是,每当我像这样进行 API 调用时:

class DetailScreen extends StatelessWidget {
// Declare a field that holds the Todo.
final Elevator elevator;
// In the constructor, require a Todo.
DetailScreen({Key key, @required this.elevator}) : super(key: key);
Future _changeStatus(elevator) async {
//Fetch the data from the api
// ignore: non_constant_identifier_names
var id = elevator.id.toString();
var holder = id.toString();
var url = 'https://rocketcrybaby.azurewebsites.net/api/elevators/'+holder;
var response = await http.put(
url, body: {'status': '${_statusController.text}'});
if (_statusController.text != "active"|| _statusController.text != "inactive"){
return "Error not a valid status !";
}
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
//print(await http.read('https://example.com/foobar.txt'));
return "sucess";
}

@override
Widget build(BuildContext context) {
// Use the Todo to create the UI.
return Scaffold(
appBar: AppBar(
title: Text("Elevator # " +elevator.id.toString()),
),
body: Center(
child: Center(
child: TextField(
controller: _statusController
),
)
),
floatingActionButton: FloatingActionButton(onPressed: () async => [ await _changeStatus(elevator.id.toString()), print(elevator.status)]),

);
}

它返回此错误:

E/flutter (18441): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: Class 'String' has no instance getter 'id'.
E/flutter (18441): Receiver: "3"
E/flutter (18441): Tried calling: id

我真的不知道该怎么办了,我尝试了书中的每一个技巧:/

供参考,这里是电梯类

class Elevator {
final String status;
final String model;
final int id;
Elevator({this.status, this.model, this.id}); //Elevator Constructor
factory Elevator.fromJson(Map<String, dynamic> json) { //Parse the infos as Json String
return Elevator(
status: json['status'],
model: json['model'],
id: json['id']
);
}

}

此外,这是我的 api 中电梯的输出


{
"id": 3,
"column_id": 1,
"serial_number": 7204019747089,
"model": "Excelium",
"building_type": "Hybrid",
"status": "Inactive",
"date_service_since": "2018-03-09T00:00:00",
"date_last_inspection": "2019-02-19T00:00:00",
"inspection_certificate": "Yes",
"information": "Dori",
"notes": "Dolores aut et. Ea optio rem. Provident exercitationem ut.",
"created_at": "2020-04-09T20:41:55",
"updated_at": "2020-04-09T20:41:55"
},

解决方案:

class DetailScreen extends StatelessWidget {
// Declare a field that holds the Todo.
final Elevator elevator;
// In the constructor, require a Todo.
DetailScreen({Key key, @required this.elevator}) : super(key: key);
Future _changeStatus(Elevator  elevator) async {
//Fetch the data from the api
// ignore: non_constant_identifier_names
var id = elevator.id.toString();
var holder = id.toString();
var url = 'https://rocketcrybaby.azurewebsites.net/api/elevators/'+holder;
var response = await http.put(
url, body: {'status': '${_statusController.text}'});
if (_statusController.text != "active"|| _statusController.text != "inactive"){
return "Error not a valid status !";
}
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
//print(await http.read('https://example.com/foobar.txt'));
return "sucess";
}

@override
Widget build(BuildContext context) {
// Use the Todo to create the UI.
return Scaffold(
appBar: AppBar(
title: Text("Elevator # " +elevator.id.toString()),
),
body: Center(
child: Center(
child: TextField(
controller: _statusController
),
)
),
floatingActionButton: FloatingActionButton(onPressed: () async => [ await _changeStatus(elevator), print(elevator.status)]),

);
}

提示:在声明变量时,请始终使用变量类型,以避免这些错误并采取良好做法。

谢谢。

那是因为你的方法:Future _changeStatus(elevator) async实际上需要Elevator elevator,但你提供的elevator.id.toString()是一个字符串。

更改方法参数的类型,并在 FAB 的 onPressed 中更改为等待_changeStatus(elevator)

相关内容

  • 没有找到相关文章

最新更新