skip to Main Content

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?

2

Answers


  1. You need to import:

    import "dart:convert";
    
    Login or Signup to reply.
  2. At the top of your file, add this import:

    import "dart:convert";
    

    as you can see from its documentation, it belongs to the dart’s convert package:

    jsonEncode

    jsonDecode

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search