Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Error: The method 'jsonEncode' is not defined

I am attempting to create a put request to an API using Flutter using the following function:

Future<http.Response> login(String username, String password) {
    return http.put(
        Uri.parse('apiurl'),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: jsonEncode(
            <String, String>{'username': username, 'password': password}));
  }

The issue I am running into is that it keeps erroring out at the jsonEncode line, saying it is not defined. I have included the following packages:

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

What am I missing to make the jsonEncode function exist?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to import:

import "dart:convert";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading