0
votes

I am trying to run this code but getting error for person.emp_id as its an int variable, can anyone help ? I have tried making string, but still I get same error, I also tried parsing in 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),)
      ],),
  ),
);
}
}
2

2 Answers

4
votes

Just change your code to

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

"person.empId" is an int value and you are assigning it to the Text widget which expects always a String value.

2
votes

When you are creating your new Person object on the MaterialPageRoute you are getting the data from json and it comes as a String, but your Person has the id defined as an int. Converting the string to an int should fix your issue:

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