flutter - 错误:无法将参数类型int分配给参数类型字符串

标签 flutter dart flutter-layout flutter-dependencies

我正在尝试运行此代码,但由于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),)
      ],),
  ),
);
}
}

最佳答案

只需将代码更改为

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

“person.empId”是一个int值,您正在将其分配给希望始终为String值的Text小部件。

关于flutter - 错误:无法将参数类型int分配给参数类型字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59423512/

相关文章:

dart - 如何对齐抽屉中扩展 block 中的子标题?

flutter - Flutter SharedPreferences getInstance返回null

Dart 不会得到依赖

flutter - 在 Flutter 中将很长的文本拆分为页面

android - 当 Flutter 抽屉里面有 Dismissible 时,如何让它关闭?

flutter - 无法在 Macbook air M1 中安装 cocoapods,每次创建 flutter 项目时 pod 文件都丢失

dart - 当方法被覆盖时如何在 Dart 中确定?

android - Flutter Fresh 项目总是在所有 Flutter Channel 中出错

unit-testing - Dart,如何使用区域进行单元测试?

android - 如何在向下滚动后到达 “SliverPersistentHeader” 的第一项之前将 “SliverList” 固定在顶部?