If anyone knows how to fix this error I'm getting would really appreciate it! I just want to use the API to access the website, saleTitle and saleCode then display it. All the code is below, hopefully, it is just a simple fix that I'm overlooking
The API for anyone wanting to see the structure of the data is at
https://script.googleusercontent.com/macros/echo?user_content_key=Y_qRVeSoWYVdwEEUjYAO42F6aIaQNXocQEXoTFW6doAyqKv5l-_jMLFTVLh40To9roCT2UfJUuF3FYokjPQiYV6f8jDWRg7rm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnIn1TJCxqQfxEAsJc2lSbT1MZbMO8nPyQvnvJosQEZ3HULcKeoNsyQ2_x4lfmdk2Asxlkl-knPMyGGaU2pP6Ntk&lib=MJriFg5Yjkb5Wmz0onstjG7LH4UjpOLQo
or the Json data here
[{"Website":"www.google.com","SaleTitle":"Up to 50% Off On All Tests!","SaleCode":"No Code Required!"},{"Website":"Website","SaleTitle":"SaleTitle","SaleCode":"SaleCode"}]
Model
/// SaleAlert is a data class which stores data fields of Sale.
class SaleAlert {
final String website;
final String saleTitle;
final String saleCode;
SaleAlert({this.website, this.saleTitle, this.saleCode});
factory SaleAlert.fromJson(Map<String, dynamic> json) {
return SaleAlert(
website: json['Website'],
saleTitle: json['SaleTitle'],
saleCode: json['SaleCode'],
);
}
}
Screen File
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:healthy_start_app/models/sale.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:http/http.dart' as http;
Future<SaleAlert> fetchAlert() async {
String url =
'https://script.google.com/macros/s/AKfycbwMcc8DHjHavJayNNJJGdfs0JNkClt3_tanyZ4p91DgcfRO58fi-5yK/exec';
final response = await http.get(url);
if (response.statusCode == 200) {
return SaleAlert.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to find latest allergy alert');
}
}
class SaleTile extends StatefulWidget {
@override
_SaleTileState createState() => _SaleTileState();
}
class _SaleTileState extends State<SaleTile> {
Future<SaleAlert> futureSaleAlert;
@override
void initState() {
super.initState();
futureSaleAlert = fetchAlert();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
},
child: Container(
width: MediaQuery.of(context).size.width * 0.95,
height: 100.0,
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
FutureBuilder<SaleAlert>(
future: futureSaleAlert,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
snapshot.data.saleTitle,
);
} else if (snapshot.hasError) {
return Text(
snapshot.error.toString(),
);
}
return Text(
'Loading...',
);
;
},
),
],
),
),
),
);
}
}