I'm newly learning Serverpod version 0.9.21 as my backend, first I changed the Client
address from http://localhost:8080/
to http://10.0.2.2:8080/
so the SocketException
error had gone on the emulator and now the sample app (say hello) works correctly. The Problem is my code faces Error 400 with no errorMessage
, Also I've added the tables and checked the database, and everything looks fine.
After creating the project, I added Province
Protocol inside the test_server package:
class: Province
table: province
fields:
name: String
isEnabled: bool
Then added the province_endpoint
:
import 'package:serverpod/serverpod.dart';
import '../generated/protocol.dart';
class ProvinceEndpoint extends Endpoint {
Future<bool> addProvince(Session session, Province province) async {
await Province.insert(session, province);
return true;
}
}
Then run serverpod generate
and start the docker and the server as tutorials are mentioning.
And finally the test_flutter main.dart
:
import 'package:back_client/back_client.dart';
import 'package:flutter/material.dart';
import 'package:serverpod_flutter/serverpod_flutter.dart';
var client = Client('http://10.0.2.2:8080/')
..connectivityMonitor = FlutterConnectivityMonitor();
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _nameController = TextEditingController(text: "name1");
String result = "here";
addProvince() async {
final province = Province(name: _nameController.text, isEnabled: true);
client.province.addProvince(province).then((value) {
if (value) {
setState(() {
result = "Done adding ${_nameController.text} province";
});
} else {
result = "Nashod!";
}
}).catchError((onError) {
final error = (onError is ServerpodClientException)
? "${onError.message} ${onError.statusCode}"
: "Error ";
setState(() {
result = error;
});
}).whenComplete(() {
print("done");
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
TextField(
controller: _nameController,
),
Text(
result,
style: Theme.of(context).textTheme.headline3,
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: addProvinces,
child: const Icon(Icons.add),
),
);
}
}
I am a noob in Backend stuff, I didn't restart the docker and server after developing the endpoint! As flutter has hot-reload it made me a little bit lazy!
After development, in the terminal that serverpod is running, press Ctrl + C
to close its processes, then run docker-compose up --build --detach && dart bin/main.dart