Skip to content

Commit

Permalink
Improved melos example with common package and todo list
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoRB committed Dec 12, 2023
1 parent 86cd0ff commit 65aa352
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
44 changes: 22 additions & 22 deletions examples/dartness_flutter_melos/apps/my_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:my_common/todo_list_response.dart';

void main() {
runApp(const MyApp());
Expand Down Expand Up @@ -37,31 +38,30 @@ class _MyHomePageState extends State<MyHomePage> {
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FutureBuilder<String>(
future: fetchHelloWorld(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.toString());
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
return const CircularProgressIndicator();
}
},
),
],
child: FutureBuilder<TodoListResponse>(
future: fetchTodos(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final result = snapshot.data!;
return ListView(
children: result.todos
.map((e) => ListTile(title: Text(e.todo)))
.toList(),
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
return const CircularProgressIndicator();
}
},
),
),
);
}

Future<String> fetchHelloWorld() async {
const myPrivateIp = "192.168.184.1";
final http.Response response =
await http.get(Uri.parse('http://$myPrivateIp:3000/hello/world'));
return response.body;
Future<TodoListResponse> fetchTodos() async {
const myPrivateIp = "192.168.2.31";
final response = await Dio().get('http://$myPrivateIp:8080/todos');
return TodoListResponse.fromJson(response.data);
}
}
6 changes: 5 additions & 1 deletion examples/dartness_flutter_melos/apps/my_app/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: my_app
description: A sample Flutter that fetch from dartness server.

publish_to: 'none'
publish_to: "none"

version: 1.0.0+1

Expand All @@ -13,11 +13,15 @@ dependencies:
sdk: flutter
cupertino_icons: ^1.0.6
http: ^1.1.0
dio: ^5.3.3
my_common:
path: ../../commons

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
json_serializable: ^6.7.1

flutter:
uses-material-design: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:my_common/todo_response.dart';
import 'package:json_annotation/json_annotation.dart';

part 'todo_list_response.g.dart';

@JsonSerializable()
class TodoListResponse {
List<TodoResponse> todos;
int total;
int skip;
int limit;

TodoListResponse({
required this.todos,
required this.total,
required this.skip,
required this.limit,
});

factory TodoListResponse.fromJson(Map<String, dynamic> json) =>
_$TodoListResponseFromJson(json);

Map<String, dynamic> toJson() => _$TodoListResponseToJson(this);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions examples/dartness_flutter_melos/commons/lib/todo_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:json_annotation/json_annotation.dart';

part 'todo_response.g.dart';

@JsonSerializable()
class TodoResponse {
int id;
String todo;
bool completed;
int userId;

TodoResponse({
required this.id,
required this.todo,
required this.completed,
required this.userId,
});

factory TodoResponse.fromJson(Map<String, dynamic> json) =>
_$TodoResponseFromJson(json);

Map<String, dynamic> toJson() => _$TodoResponseToJson(this);
}
22 changes: 22 additions & 0 deletions examples/dartness_flutter_melos/commons/lib/todo_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/dartness_flutter_melos/commons/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: my_common
description: A sample dartness server.
version: 1.0.0
# homepage: https://www.example.com

publish_to: "none"

environment:
sdk: ">=3.0.0 <4.0.0"

dev_dependencies:
json_serializable: ^6.7.1
build_runner: ^2.4.6
17 changes: 15 additions & 2 deletions examples/dartness_flutter_melos/servers/my_server/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import 'package:dartness_server/dartness.dart';
import 'package:dartness_server/modules.dart';
import 'package:dartness_server/server.dart';
import 'package:dio/dio.dart';

import 'example_controller.dart';
import 'hello_world_controller.dart';
import 'todos_controller.dart';

part 'app.g.dart';

Dio createDio() => Dio();

@Application(
module: Module(
metadata: ModuleMetadata(
controllers: [
ProviderMetadata(
classType: ExampleController,
),
ProviderMetadata(
classType: TodosController,
),
],
providers: [
ProviderMetadata(
classType: Dio,
useFactory: createDio,
),
],
),
),
options: DartnessApplicationOptions(
port: int.fromEnvironment(
'port',
defaultValue: 3000,
defaultValue: 8080,
),
),
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:dartness_server/route.dart';

part 'example_controller.g.dart';
part 'hello_world_controller.g.dart';

@Controller("/hello")
class ExampleController {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:io';

import 'package:dartness_server/route.dart';
import 'package:dio/dio.dart';
import 'package:my_common/todo_list_response.dart';
import 'dart:math';

part 'todos_controller.g.dart';

@Controller('/todos')
@Header(HttpHeaders.contentTypeHeader, 'application/json')
class TodosController {
final Dio _dio;

TodosController(this._dio);

@Get()
Future<TodoListResponse> getTodos() async {
var rng = Random();
final userId = rng.nextInt(6);
final result = await _dio.get('https://dummyjson.com/todos/user/$userId');
return TodoListResponse.fromJson(result.data);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ description: A sample dartness server.
version: 1.0.0
# homepage: https://www.example.com

publish_to: 'none'
publish_to: "none"

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
dartness_server: ^0.6.0
dio: ^5.3.3
my_common:
path: ../../commons

dev_dependencies:
lints: ^3.0.0
Expand Down

0 comments on commit 65aa352

Please sign in to comment.