w461
w461

Reputation: 2698

FLUTTER: fromJson and parsedJson method not found, although import 'dart:convert';

I just started with flutter and I don't find a solution to this problem. I am currently starting off with the flutter demo provided and trying to convert this step by step to a Cupertino app.

I also used this nice read to implement JSON. However, even though I include dart:convert, the JSON methods are not found. The error is

lib/services/service_textproblem.dart:13:45: Error: Method not found: 'TextProblem.fromJson'.
  TextProblem textproblem = new TextProblem.fromJson(jsonResponse);
                                            ^^^^^^^^
lib/model/model_textproblem.dart:42:16: Error: Getter not found: 'parsedJson'.
    var list = parsedJson['taskPrmtrs'] as List;
               ^^^^^^^^^^
lib/model/model_textproblem.dart:44:12: Error: Getter not found: 'parsedJson'.
    list = parsedJson['taskTags'] as List;
           ^^^^^^^^^^

My service_textproblem.dart looks like this and dart:convert is not highlighted as not being used (other way around, highlighted as being used)

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:math/model/model_textproblem.dart';

Future<String> _loadTextProblemAsset() async {
  return await rootBundle.loadString('assets/textproblems.json');
}

Future loadTextProblems() async {
  String jsonString = await _loadTextProblemAsset();
  final jsonResponse = json.decode(jsonString);
  TextProblem textproblem = new TextProblem.fromJson(jsonResponse);
  print(textproblem.problemTasks[0].taskText);
}

Any ideas what I can do? I couldn't find any similar issue (except people not having included dart:convert)

Upvotes: 0

Views: 4237

Answers (2)

Tipu Sultan
Tipu Sultan

Reputation: 1865

fromJson and parsedJson method not found, although import 'dart:convert';

fromJson and parsedJson is not a property of dart:convert class. You need to implement those method in your Model class. Here is an example of a Model class.

class Model {
  int id;
  String name;

  Model({this.id, this.name});

  Model.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}

In this class Model.dromJson function convert the json object in Model object and toJson convert the model object in json format. You need to manually add these function in your TextProblem class.

There are many website which convert json into data class. You can try this one.

Upvotes: 1

You should write your own TextProblem model class like the guideline

class User {
  final String name;
  final String email;

  User(this.name, this.email);

  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];

  Map<String, dynamic> toJson() =>
    {
      'name': name,
      'email': email,
    };
}

Upvotes: 0

Related Questions