Created
September 19, 2020 17:17
-
-
Save JasCodes/c254a8cba9cfb19c019ccfd56aa36da1 to your computer and use it in GitHub Desktop.
GQLess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
class Pokemon extends StatelessWidget { | |
final pokemon = Pokemon(); | |
@override | |
Widget build(context) { | |
return Scaffold( | |
appBar: AppBar(title: Text("Pokemon")), | |
body: Center( | |
child: Obx(() { | |
return Column( | |
children: pokemon.list | |
// .map((pokemon) => Text("${pokemon.id}")) | |
.map((pokemon) => Text("${pokemon.id} ${pokemon.name}")) | |
.toList() | |
..add(Text(pokemon.gql())), | |
); | |
}), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () { | |
pokemon.query(); | |
}, | |
), | |
); | |
} | |
} | |
class Pokemon { | |
RxList<Pokemon> list; | |
int _id; | |
String _name; | |
final returnMap = Map<String, bool>(); | |
Pokemon() { | |
list = [this].obs; | |
} | |
int get id { | |
returnMap["id"] = true; | |
return _id; | |
} | |
set id(int id) { | |
_id = id; | |
} | |
String get name { | |
returnMap["name"] = true; | |
return _name; | |
} | |
set name(String name) { | |
_name = name; | |
} | |
String gql() { | |
var fields = ""; | |
returnMap.forEach((field, used) { | |
if (used) fields += "\t$field\n"; | |
}); | |
return "\nquery pokemon {\n$fields}"; | |
} | |
query() { | |
print(gql()); | |
// make graphql query using returnMap | |
list.value = [ | |
Pokemon() | |
..id = 12345 | |
..name = "Pikachu", | |
Pokemon() | |
..id = 21234 | |
..name = "Jiggly Puff", | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment