skip to Main Content

I created this class for fetch api and I can’t get textForm from my textinput another class.

class RemoteService {
  
   Future<List<Member>?> getUser({String? textForm}) async {
    var clientuser = http.Client();
    var uriuser = Uri.parse('HTTP://MY URI//$textForm');
    var responseuse = await clientuser.get(uriuser);
    if (responseuse.statusCode == 200) {
      var json = responseuse.body;
      return memberFromJson(json);
    }
  }
}

Main Class, I try to send textFieldValue.text to RemoteService to get data but when I compile my code, it can’t get my value from my textfield.

class MyCustomFormState extends State<MyCustomForm> {
  List<Member>? user1;
  var isLoaded = false;
  TextEditingController textFieldValue= TextEditingController();
  late String getusevalue;

  @override
  void initState() {
    super.initState();
    getUse();
  }

  getUse() async {
    user1 = await RemoteService().getUser(textForm: textFieldValue.text);
    if (user1 != null) {
      setState(() {
        isLoaded = true;
      });
    }
  }
  
  @override
  Widget build(BuildContext context) {
Container(
                  width: 200,
                  child:
                  TextFormField(
                   controller: textFieldValue,
                    decoration: InputDecoration(
                                border: OutlineInputBorder(),
                                fillColor: Color.fromARGB(255, 255, 255, 255),
                                hintText: 'PLEASE ENTER ID',
                              ), 
                    onChanged: (text) {  
                    getusevalue = textFieldValue.text;
                   
                  },  
                  )
                        
                ),
               Container(
                            width: 120,
                            height: 50,
                            margin: EdgeInsets.only(top: 15),
                          child: ElevatedButton(
                            child: Text("LOGIN",style: TextStyle(
                                      fontSize: 16, fontWeight: FontWeight.bold),),
                            style: ElevatedButton.styleFrom(
                              primary: Color.fromARGB(255, 45, 160, 253),
                              elevation: 0,
                            ), onPressed: () {
                              print("INPUT = "+getusevalue);
                              print("Fetch API = ${user1?[0].name}");
                            },
                            )
                            ),

Error here:

Performing hot restart...                                               
Restarted application in 2,712ms.
E/flutter (13579): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '(dynamic) => Member' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'
E/flutter (13579): #0      memberFromJson (package:flutter_exit_interview/dtbmember.dart:8:79)
E/flutter (13579): #1      RemoteService.getUser (package:flutter_exit_interview/main.dart:1061:14)
E/flutter (13579): <asynchronous suspension>
E/flutter (13579): #2      MyCustomFormState.getUse (package:flutter_exit_interview/main.dart:83:13)
E/flutter (13579): <asynchronous suspension>
E/flutter (13579):

And this is my model:

// To parse this JSON data, do
//
//     final member = memberFromJson(jsonString);

import 'package:meta/meta.dart';
import 'dart:convert';

List<Member> memberFromJson(String str) => List<Member>.from(json.decode(str).map((x) => Member.fromJson(x)));
String memberToJson(List<Member> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Member {
    Member({
        required this.status,
        required this.personId,
        required this.code,
        required this.name,
        required this.nickname,
        required this.deptcode,
    });

    String status;
    String personId;
    String code;
    String name;
    String nickname;
    String deptcode;

    factory Member.fromJson(Map<String, dynamic> json) => Member(
        status: json["status"],
        personId: json["person_id"],
        code: json["code"],
        name: json["name"],
        nickname: json["nickname"],
        deptcode: json["deptcode"],
    );

    Map<String, dynamic> toJson() => {
        "status": status,
        "person_id": personId,
        "code": code,
        "name": name,
        "nickname": nickname,
        "deptcode": deptcode,
    };
}

2

Answers


  1. Chosen as BEST ANSWER

    Now i try this it work.

    getUse() async {
      String getusevalue = 'P3116'; //setvalue not from my input
      user1 = await RemoteService().getUser(textForm: '${getusevalue}');
      if (user1 != null) {
        setState(() {
          isLoaded = true;
        });
      }
    }
    

    But last time I try this it not work:

    getUse() async {
      String getusevalue = textFieldValue.text; //set value from my input!
      user1 = await RemoteService().getUser(textForm: '${getusevalue}');
      if (user1 != null) {
        setState(() {
          isLoaded = true;
        });
      }
    }
    

  2. Try with these changes:

    String getusevalue = '';
    setState(() {
      getusevalue = textFieldValue.text;
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search