Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active March 14, 2023 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/d185fdda70e2bec2d183575e58a6db0e to your computer and use it in GitHub Desktop.
Save gistlyn/d185fdda70e2bec2d183575e58a6db0e to your computer and use it in GitHub Desktop.
flutter
flutter create my_app_flutter
dart pub add servicestack --directory my_app_flutter
dart pub get --directory my_app_flutter
/* Options:
Date: 2023-01-13 13:45:10
Version: 6.41
Tip: To override a DTO option, remove "//" prefix before updating
BaseUrl: https://localhost:5001
//GlobalNamespace:
//AddServiceStackTypes: True
//AddResponseStatus: False
//AddImplicitVersion:
//AddDescriptionAsComments: True
//IncludeTypes:
//ExcludeTypes:
//DefaultImports: package:servicestack/servicestack.dart
*/
import 'package:servicestack/servicestack.dart';
class HelloResponse implements IConvertible
{
String? result;
HelloResponse({this.result});
HelloResponse.fromJson(Map<String, dynamic> json) { fromMap(json); }
fromMap(Map<String, dynamic> json) {
result = json['result'];
return this;
}
Map<String, dynamic> toJson() => {
'result': result
};
getTypeName() => "HelloResponse";
TypeContext? context = _ctx;
}
// @Route("/hello")
// @Route("/hello/{Name}")
class Hello implements IReturn<HelloResponse>, IConvertible
{
String? name;
Hello({this.name});
Hello.fromJson(Map<String, dynamic> json) { fromMap(json); }
fromMap(Map<String, dynamic> json) {
name = json['name'];
return this;
}
Map<String, dynamic> toJson() => {
'name': name
};
createResponse() => HelloResponse();
getResponseTypeName() => "HelloResponse";
getTypeName() => "Hello";
TypeContext? context = _ctx;
}
TypeContext _ctx = TypeContext(library: 'localhost', types: <String, TypeInfo> {
'HelloResponse': TypeInfo(TypeOf.Class, create:() => HelloResponse()),
'Hello': TypeInfo(TypeOf.Class, create:() => Hello()),
});
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:servicestack/web_client.dart' if (dart.library.io) 'package:servicestack/client.dart';
import 'dart:io';
import 'dtos.dart';
var baseUrl = "https://localhost:5001";
var clientOptions = ClientOptions(baseUrl: baseUrl);
var client = ClientFactory.createWith(clientOptions);
void main() {
if (!kReleaseMode && !kIsWeb) {
if (Platform.isAndroid) {
clientOptions.baseUrl = "https://10.0.2.2:5001";
clientOptions.ignoreCertificatesFor.add(clientOptions.baseUrl);
}
}
client = ClientFactory.createWith(clientOptions);
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(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const HelloFlutter(title: 'Flutter Demo Home Page'),
);
}
}
class HelloFlutter extends StatefulWidget {
const HelloFlutter({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<StatefulWidget> createState() => HelloFlutterState();
}
class HelloFlutterState extends State<HelloFlutter> {
//State for this widget
String result = "";
var myController = TextEditingController();
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
void initState() {
super.initState();
myController.addListener(callService);
}
void callService() async {
var text = myController.text.isEmpty ? "World" : myController.text;
var response = await client.get(Hello(name: text));
setState(() {
result = response.result!;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
Container(
padding: const EdgeInsets.only(top: 20),
margin: const EdgeInsets.only(left: 100.0, right: 100.0),
child: Column(
children: [
const Text(
'Hello API',
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 24.0),
),
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'World',
),
controller: myController,
)
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Result: $result',
style: const TextStyle(fontSize: 18),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app_flutter/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment