Flutter 我能';t序列化在flatter中有下划线的数据

Flutter 我能';t序列化在flatter中有下划线的数据,flutter,Flutter,我是一个新手,我正在尝试从API获取一个数据列表,我对类似“特色图像”的所有字段都有问题。我可以从API中正确地获取数据,但当我解析数据时,只有将字段“featured_image”设置为Null时,它才起作用,否则什么也不会发生。我怎样才能解决这个问题 下面是我的代码: @JsonSerializable() class Recipe { final int pk; final String title; final String publisher; final String

我是一个新手,我正在尝试从API获取一个数据列表,我对类似“特色图像”的所有字段都有问题。我可以从API中正确地获取数据,但当我解析数据时,只有将字段“featured_image”设置为Null时,它才起作用,否则什么也不会发生。我怎样才能解决这个问题

下面是我的代码:

@JsonSerializable()
class Recipe {
  final int pk;
  final String title;
  final String publisher;
  final String featuredImage;
  final int rating;
  final Null sourceUrl;
  final List<String> ingredients;
  final Null dateAdded;
  final Null dateUpdated;

  Recipe(
    this.pk,
    this.title,
    this.publisher,
    this.featuredImage,
    this.rating,
    this.sourceUrl,
    this.ingredients,
    this.dateAdded,
    this.dateUpdated,
  );

  String get getTitle => title;

  factory Recipe.fromJson(Map<String, dynamic> data) => _$RecipeFromJson(data);
}

你到底有什么问题?你说什么都没发生是什么意思?你是说它是空的吗?我检查了你的代码并运行了它,它运行得很好。我在dartpad中进行了测试,并在中进行了共享,正如您在NetworkService中看到的那样,我编写了几张打印图来查看输出。当我设置“final Null featuredImage”时,它在图像打印中返回Null,正如预期的那样。当我将其设置为“final String featuredImage”时,图像打印没有运行。我复制并粘贴了Recipe类,它成功了。也许是JSONA符号的问题
Recipe _$RecipeFromJson(Map<String, dynamic> json) {
  return Recipe(
    json['pk'] as int,
    json['title'] as String,
    json['publisher'] as String,
    json['featured_image'] as String,
    json['rating'] as int,
    json['sourceUrl'] as Null,
    (json['ingredients'] as List<dynamic>).map((e) => e as String).toList(),
    json['dateAdded'] as Null,
    json['dateUpdated'] as Null,
  );
}
class NetworkService {
  Future<List<Recipe>> getRecipes() async {
    var response = await http.get(
      Uri.parse('https://food2fork.ca/api/recipe/search/?page=1&query='),
      headers: {
        HttpHeaders.authorizationHeader:
            'Token 9c8b06d329136da358c2d00e76946b0111ce2c48',
      },
    );

    final List<Recipe> recipeList = [];
    final responseJson = jsonDecode(response.body);
    print('response: ${responseJson['results']}');
    for (Map<String, dynamic> recipe in responseJson['results']) {
      recipeList.add(
                Recipe.fromJson(recipe),
      );
    }

    print('image: ${recipeList[0].featuredImage}');
    return recipeList;
  }
}
{
    "count": 118,
    "next": "http://127.0.0.1:8000/api/recipe/search/?page=3&query=beef+carrot+potato+onion",
    "previous": "https://food2fork.ca/api/recipe/search/?query=beef+carrot+potato+onion",
    "results": [
        {
            "pk": 583,
            "title": "Pizza Potato Skins",
            "publisher": "mitch",
            "featured_image": "https://nyc3.digitaloceanspaces.com/food2fork/food2fork-static/featured_images/583/featured_image.png",
            "rating": 16,
            "source_url": "http://thepioneerwoman.com/cooking/2013/04/pizza-potato-skins/",
            "description": "N/A",
            "cooking_instructions": null,
            "ingredients": [
                "Canola Oil",
                "Kosher Salt",
                "Butter, Melted",
                "Diced Pepperoni",
                "Minced Fresh Parsley",
                "Grated Mozzarella Cheese",
                "8 whole Small Russet Potatoes",
                "Jarred Marinara Or Pizza Sauce",
                "Miscellaneous Pizza Toppings: Cooked Sausage, Cooked Hamburger, Diced Bell Pepper, Diced Onion, Diced Mushrooms, Diced Canadian Bacon, Etc."
            ],
            "date_added": "November 11 2020",
            "date_updated": "November 11 2020",
            "long_date_added": 1606349126,
            "long_date_updated": 1606349126
        }
    ]
}