Olá! No projeto de uma aula, fizemos atualizações em um gist usando post. Dá statusCode 200 e ao atulizar o gist no navegador aparece a modificação, mas quando acesso depois, tudo voltou ao que era antes. Tenho que fazer algo como um commit?
O código:
import 'dart:async';
import 'dart:convert';
import 'package:api/api_key.dart';
import 'package:http/http.dart';
void main() {
//requestData();
//requestDataAsync();
sendDataAsync({
"id": "new002",
"name": "Flutter",
"lastname": "Dart",
"balance": "85800",
});
}
requestData() {
String url =
"https://gist.githubusercontent.com/cbvasconcellos/2853ba1565c41b96ee221bcf0dd00560/raw/a58692a72f579721b484ee4a03ddff3c4a016082/accounts.json";
Future<Response> futureResponse = get(Uri.parse(url)); //assíncrona
//qdo o carregamento acabar
futureResponse.then((Response response) {
print(response);
print(response.body);
List<dynamic> listAccounts = json.decode(response.body);
Map<String, dynamic> mapCarla = listAccounts.firstWhere(
(element) => element["name"] == "Carla",
);
print(mapCarla["balance"]);
});
print("ultima coisa a acontecer");
}
Future<List<dynamic>> requestDataAsync() async {
//async informa que é uma fx assíncrona
String url =
"https://gist.githubusercontent.com/cbvasconcellos/2853ba1565c41b96ee221bcf0dd00560/raw/a58692a72f579721b484ee4a03ddff3c4a016082/accounts.json";
Response response = await get(
Uri.parse(url),
);
return json.decode(response.body);
}
sendDataAsync(Map<String, dynamic> mapAccount) async {
List<dynamic> listAccounts = await requestDataAsync();
listAccounts.add(mapAccount);
String content = json.encode(listAccounts);
//print(content);
String url = "https://api.github.com/gists/2853ba1565c41b96ee221bcf0dd00560";
Response response = await post(
Uri.parse(url),
headers: {"Authorization": "Bearer $githubApiKey"},
body: json.encode({
"description": "account.json",
"public": true,
"files": {
"accounts.json": {"content": content},
},
}),
);
print(response.statusCode);
}