Skip to content

Instantly share code, notes, and snippets.

@Arham4
Last active March 23, 2019 14:48
Show Gist options
  • Save Arham4/a36a05131d3c3edb4a561efc276f9399 to your computer and use it in GitHub Desktop.
Save Arham4/a36a05131d3c3edb4a561efc276f9399 to your computer and use it in GitHub Desktop.
Simple Python and Flutter Communication
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() {
stuff();
}
void stuff() {
var thing = fetchOmg();
thing.then((omg) {
print('omg we got ${omg.age} and ${omg.name}');
});
}
Future<Omg> fetchOmg() async {
final response =
await http.get('http://10.0.2.2:8000/omg');
if (response.statusCode == 200) {
return Omg.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load');
}
}
class Omg {
final String name;
final int age;
Omg({this.name, this.age});
factory Omg.fromJson(Map<String, dynamic> json) {
return Omg(
name: json['name'],
age: json['age']
);
}
}
import os
import jsonpickle
from flask import Flask
app = Flask(__name__)
@app.route("/omg")
def omg():
omg = Omg('test', 10)
return str(jsonpickle.encode(omg, unpicklable=False)) # unpicklable=False is needed for us to make a JSON that is universally readable
class Omg:
def __init__(self, name, age):
self.name = name
self.age = age
if __name__ == "__main__":
app.run(host='localhost', port=os.environ.get('PORT', 8000), debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment