You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发中jsonEncode方法未定义问题求助

解决Flutter中jsonEncode未定义的问题

jsonEncode 并非你当前导入的flutter/material.dart或http包中的方法,它属于Dart标准库的dart:convert模块,必须显式导入该模块才能正常使用。

解决方法:

  • 在文件顶部添加以下导入语句:
    import 'dart:convert';
    
  • 若担心命名冲突,可给库添加前缀:
    import 'dart:convert' as convert;
    
    此时调用时需改为 convert.jsonEncode(...)

修改后的完整代码示例:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert'; // 新增导入

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}));
}

内容的提问来源于stack exchange,提问作者KosherHotDog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 00:16:14