【问题标题】:Error : the argument type int cannot be assigned to the parameter type string错误:参数类型 int 不能分配给参数类型字符串
【发布时间】:2020-04-12 20:45:19
【问题描述】:

我正在尝试运行此代码,但将 person.emp_id 作为一个 int 变量出现错误,有人可以帮忙吗? 我试过制作字符串,但我仍然得到同样的错误,我也尝试在 int 中解析 error: The argument type '(int) → dynamic' can't be assigned to the parameter type '(String) → void'

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';

void main() {
    runApp(MaterialApp(
        home: MyGetHttpData(),
    ));
}


class MyGetHttpData extends StatefulWidget {
    @override
    MyGetHttpDataState createState() => MyGetHttpDataState();
}


class MyGetHttpDataState extends State<MyGetHttpData> {
    final String url = "https://raw.githubusercontent.com/uc-ach/flutter/master/test.json";
    List data;


    Future<String> getJSONData() async {
    var response = await http.get(
      Uri.encodeFull(url),
      headers: {"Accept": "application/json"});


      print(response.body);


setState(() {

  var dataConvertedToJSON = json.decode(response.body);

  data = dataConvertedToJSON;
});

return "Successfull";
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Inside Sales User List"),
  ),

  body: ListView.builder(
      itemCount: data == null ? 0 : data.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          child: Center(
              child: Column(

                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Card(
                    child: Container(
                      child: ListTile(
                          title: Text(

                            data[index]['name'],

                            style: TextStyle(
                                fontSize: 22.0, color: Colors.lightBlueAccent),
                          ),
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => new SecondRoute(person: new Person(data[index]['name'],data[index]['post'],data[index]['emp_id']))),
                            );
                          },
                      ),

                      padding: const EdgeInsets.all(15.0),
                    ),
                  )
                ],
              )),
        );
      }),
);
}
@override
void initState() {
super.initState();


this.getJSONData();
}
}
class Person {
    final String name;
    final String post;
    int empId;
    Person(this.name, this.post, this.empId);
}
class SecondRoute extends StatelessWidget {
    final Person person;
   SecondRoute({Key key, @required this.person}) : super(key: key);
   @override
   Widget build(BuildContext context) {
   return Scaffold(
   appBar: AppBar(
    title: Text("Details for " +person.name),
   ),
  body: Padding(
    padding: EdgeInsets.all(16.0),

      child: Column( children: <Widget>[
  Text("Name: " +person.name, style: TextStyle(
      fontSize: 20.0),),
    Text("Emp Id: " +person.empId, style: TextStyle(
        fontSize: 20.0),)
      ],),
  ),
);
}
}

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies


    【解决方案1】:

    只需将代码更改为

    Text("Emp Id: " + person.empId.toString(), style: TextStyle(fontSize: 20.0),)
    

    "person.empId" 是一个 int 值,您将其分配给 Text 小部件,该小部件始终需要一个 String 值。

    【讨论】:

      【解决方案2】:

      当您在 MaterialPageRoute 上创建新的 Person 对象时,您从 json 获取数据,它以字符串的形式出现,但您的 Person 将 id 定义为 int。将字符串转换为 int 应该可以解决您的问题:

      MaterialPageRoute(builder: (context) => new SecondRoute(
        person: new Person(
          data[index]['name'],
          data[index]['post'],
          int.parse(data[index]['emp_id'])
        )
      )),
      

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 2019-11-15
        • 1970-01-01
        • 2021-09-03
        • 2021-10-10
        • 1970-01-01
        • 2021-10-21
        • 2021-10-13
        相关资源
        最近更新 更多