Home > Software design >  The method 'getContacts' isn't defined for the type 'Type'
The method 'getContacts' isn't defined for the type 'Type'

Time:04-26

I'm new to the GetX framework and am trying the call the controller class function from the view using the Elevated button but it causes an error.

The method 'getContacts' isn't defined for the type 'Type'.

this is my contoller class

  class ContactController extends GetxController {
  var id;

  @override
  void onInit() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    id = preferences.getString('id');
    print(id);
  }

  getContact() async {
    // SERVER API URL

    Uri url = Uri.parse('http://localhost:8000/get-contacts');

    // Store all data with Param Name.
    var data = {'sender': id};

    //json encode
    String? body = json.encode(data);

    // Starting Web API Call.
    var response = await http.post(url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: body);

    // Getting Server response into variable.
    var contact = jsonDecode(response.body);

    // If Web call Success than Hide the CircularProgressIndicator.
    if (response.statusCode == 200) {
      print(contact);
    } else {
      print("error");
    }
  }
}

this is my view class

class Contacts extends StatefulWidget {
  const Contacts({Key? key}) : super(key: key);

  @override
  State<Contacts> createState() => _ContactsState();
}

class _ContactsState extends State<Contacts> {
  final contactController = Get.put((ContactController));

  @override
  Widget build(BuildContext context) {
    return Container(
        child: ElevatedButton(
            onPressed:()=>contactController.getContacts(),
            
            child: Text("GET CONTACT")));
  }
}

I can't able to call the function inside the elevated Button

onPressed:()=>contactController.getContacts(),

any one help me to solve this issue

CodePudding user response:

Try changing it

final contactController = Get.put(ContactController());
  • Related