Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chapter_06/lib/controllers/plan_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PlanController {
services.delete(plan);
}

void createNewTask(Plan plan, [String description]) {
void createNewTask(Plan plan, [String? description]) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I leave it as String description, then I have a conflict on controller.createNewTask(plan); in _buildAddTaskButton function because we are not sending description in there, so that's I made this to be optional. However, I think there might be better way to handle this.

if (description == null || description.isEmpty) {
description = 'New Task';
}
Expand Down
10 changes: 6 additions & 4 deletions chapter_06/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import 'package:flutter/material.dart';
import 'plan_provider.dart';
import 'views/plan_creator_screen.dart';

void main() => runApp(PlanProvider(child: MasterPlanApp()));
void main() => runApp(PlanProvider(child: const MasterPlanApp()));

class MasterPlanApp extends StatelessWidget {
const MasterPlanApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.purple),
home: PlanCreatorScreen(),
theme: ThemeData(primarySwatch: Colors.purple),
home: const PlanCreatorScreen(),
);
}
}
}
2 changes: 1 addition & 1 deletion chapter_06/lib/models/data_layer.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export 'plan.dart';
export 'task.dart';
export 'task.dart';
9 changes: 4 additions & 5 deletions chapter_06/lib/models/plan.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import '../repositories/repository.dart';
import 'package:flutter/foundation.dart';
import './task.dart';

class Plan {
final int id;
String name = '';
String name;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this assignment might be redundant as we are assigning it in the constructor on line 9.

List<Task> tasks = [];

Plan({@required this.id, this.name = ''});
Plan({required this.id, this.name = ''});

Plan.fromModel(Model model)
: id = model.id,
name = model?.data['name'],
tasks = model?.data['task']
name = model.data['name'] ?? '',
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting type 'Null' is not a subtype of type 'String', so I decided to solve this way.

tasks = model.data['tasks']
?.map<Task>((task) => Task.fromModel(task))
?.toList() ??
<Task>[];
Expand Down
3 changes: 1 addition & 2 deletions chapter_06/lib/models/task.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import '../repositories/repository.dart';
import 'package:flutter/foundation.dart';

class Task {
final int id;
String description;
bool complete;

Task({
@required this.id,
required this.id,
this.complete = false,
this.description = '',
});
Expand Down
10 changes: 6 additions & 4 deletions chapter_06/lib/plan_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import 'controllers/plan_controller.dart';
class PlanProvider extends InheritedWidget {
final _controller = PlanController();

PlanProvider({Key key, Widget child}) : super(key: key, child: child);

@override
bool updateShouldNotify(InheritedWidget oldWidget) => false;
bool updateShouldNotify(covariant InheritedWidget oldWidget) => false;

PlanProvider({Key? key, required Widget child})
: super(key: key, child: child);

static PlanController of(BuildContext context) {
PlanProvider provider = context.dependOnInheritedWidgetOfExactType<PlanProvider>();
final provider = context.dependOnInheritedWidgetOfExactType<PlanProvider>()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I specify PlanProvider, I get an error A value of type 'PlanProvider?' can't be assigned to a variable of type 'PlanProvider'.

as PlanProvider;
return provider._controller;
}
}
6 changes: 3 additions & 3 deletions chapter_06/lib/repositories/in_memory_cache.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import 'repository.dart';

class InMemoryCache implements Repository {
final _storage = Map<int, Model>();
final _storage = <int, Model>{};

@override
Model create() {
final ids = _storage.keys.toList()..sort();

final id = (ids.length == 0) ? 1 : ids.last + 1;
final id = (ids.isEmpty) ? 1 : ids.last + 1;

final model = Model(id: id);
_storage[id] = model;
return model;
}

@override
Model get(int id) {
Model? get(int id) {
return _storage[id];
}

Expand Down
8 changes: 3 additions & 5 deletions chapter_06/lib/repositories/repository.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:flutter/foundation.dart';

abstract class Repository {
Model create();

List<Model> getAll();
Model get(int id);
Model? get(int id);
void update(Model item);

void delete(Model item);
Expand All @@ -16,7 +14,7 @@ class Model {
final Map data;

const Model({
@required this.id,
required this.id,
this.data = const {},
});
}
}
7 changes: 5 additions & 2 deletions chapter_06/lib/services/plan_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '../repositories/in_memory_cache.dart';
import '../repositories/repository.dart';

class PlanServices {
Repository _repository = InMemoryCache();
final Repository _repository = InMemoryCache();

Plan createPlan(String name) {
final model = _repository.create();
Expand All @@ -28,7 +28,10 @@ class PlanServices {
}

void addTask(Plan plan, String description) {
final id = plan.tasks.last?.id ?? 0 + 1;
Copy link
Copy Markdown
Author

@bissenbay bissenbay Oct 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I had this conflict The receiver can't be null, so the null-aware operator '?.' is unnecessary.. However, if I remove ?. before id, then I get The left operand can't be null, so the right operand is never executed.. After that, the suggested looks like this final id = plan.tasks.last.id;. However, it breaks my code, so I actually came up with below code.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you found any solution to this issue?

Copy link
Copy Markdown
Author

@bissenbay bissenbay Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amkfat lines 31 to 34 are the solution I came up with

int id = 0;
if (plan.tasks.isNotEmpty) {
id = plan.tasks.last.id + 1;
}
final task = Task(id: id, description: description);
plan.tasks.add(task);
savePlan(plan);
Expand Down
28 changes: 19 additions & 9 deletions chapter_06/lib/views/plan_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@ class _PlanScreenState extends State<PlanScreen> {

@override
Widget build(BuildContext context) {
// final plan = PlanProvider.of(context);
return Scaffold(
appBar: AppBar(title: Text('Master Plan')),
body: Column(children: <Widget>[
Expanded(child: _buildList()),
SafeArea(child: Text(plan.completenessMessage))
]),
floatingActionButton: _buildAddTaskButton());
return WillPopScope(
onWillPop: () {
final controller = PlanProvider.of(context);
controller.savePlan(plan);
return Future.value(true);
},
child: Scaffold(
appBar: AppBar(
title: const Text('Master Plan'),
),
body: Column(
children: [
Expanded(child: _buildList()),
SafeArea(child: Text(plan.completenessMessage))
],
),
floatingActionButton: _buildAddTaskButton(),
),
);
}

@override
Expand All @@ -53,7 +64,6 @@ class _PlanScreenState extends State<PlanScreen> {
}

Widget _buildList() {
//final plan = PlanProvider.of(context);
return ListView.builder(
controller: scrollController,
itemCount: plan.tasks.length,
Expand Down