Chapter 5 Error

When I running it’s show message error.

  • ‘Object’ is from ‘dart:core’.
    Try correcting the name to the name of an existing getter, or defining a getter or field named ‘todayRecipes’.
    TodayRecipeListView(recipes: snapshot.data.todayRecipes),

How to fix it?

All Code below

import ‘package:flutter/material.dart’;
import ‘…/components/friend_post_list_view.dart’;
import ‘…/api/mock_fooderlich_service.dart’;
import ‘…/components/components.dart’;

class ExploreScreen extends StatelessWidget {
final mockService = MockFooderlichService();

ExploreScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: mockService.getExploreData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView(scrollDirection: Axis.vertical, children: [
TodayRecipeListView(recipes: snapshot.data.todayRecipes),
const SizedBox(height: 16),
// FriendPostListView(friendPosts: snapshot.data.friendPosts)
]);
} else {
return const Center(child: CircularProgressIndicator());
}
});
}
}

@namdh1 just went through the chapter 5 sample projects, and everything looks fine.

Two points for you:

  1. Do you have null safety enabled? (We are in the process of converting all our projects to sound null safety)
  2. If you can check this into github, i can take a closer look.

Thanks!

Hi ,

This is source code in gitlab you can review

Thanks

@namdh1 Flutter Apprentice is in the process of migrating to null safety. The project you are working on has null safety enabled which we currently don’t support.

As shown in the screenshot below

Screen Shot 2021-08-16 at 8.47.13 PM

For more information on how to migrate or work with null safety please take a look at:

Here is the temporary fix till the book is updated:

From your code:

  1. Make sure you remove import 'dart:html'; from today_recipe_list_view.dart
  2. TodayRecipeListView takes in a List not an optional List?
    You need to make sure data is not null. By adding !:

As shown below:
We know that todayRecipes is not null:

snapshot.data!.todayRecipes

ExploreScreen should look something like this:

import 'package:flutter/material.dart';
import 'package:fooderlich/components/today_recipe_list_view.dart';
import 'package:fooderlich/models/models.dart';
import '../api/mock_fooderlich_service.dart';
import '../components/components.dart';

class ExploreScreen extends StatelessWidget {
  final mockService = MockFooderlichService();
  ExploreScreen({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: mockService.getExploreData(),
        builder: (context, AsyncSnapshot<ExploreData> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return ListView(scrollDirection: Axis.vertical, children: [
              TodayRecipeListView(recipes: snapshot.data!.todayRecipes),
              const SizedBox(height: 16),
              Container(
                height: 400,
                color: Colors.green,
              ),
            ]);
            //   final recipes = snapshot.data.todayRecipes;
            //    print("===============>" + snapshot.data.toString());

            // return TodayRecipeListView(recipes: recipes);
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        });
  }
}

hi jomoka,

I try to fix with your guide but run still error. I’Error
ve push code fix you can review

@namdh1 notice in your code you did not give snapshot a type.

return FutureBuilder(
        future: mockService.getExploreData(),
        builder: (context, AsyncSnapshot<ExploreData> snapshot) {

You need to assign snapshot a type object of ExploreData. It doesn’t know what object to cast to.
Please try this and let me know thanks!

It’s running OK,

Thanks for helping me

1 Like

Np let me know if you need anymore help