Generate complete endpoint URLs in the provider

This commit is contained in:
Roland Geider
2020-11-05 12:15:55 +01:00
parent 722fea70a3
commit 2de9708934
3 changed files with 21 additions and 10 deletions

View File

@@ -30,7 +30,7 @@ class MyApp extends StatelessWidget {
ChangeNotifierProxyProvider<Auth, WorkoutPlans>(
create: null, // TODO: Create is required but it can be null??
update: (context, value, previous) => WorkoutPlans(
auth.token,
auth,
previous == null ? [] : previous.items,
),
)

View File

@@ -29,6 +29,10 @@ class Auth with ChangeNotifier {
// return null;
}
String get serverUrl {
return _serverUrl;
}
// String get userId {
// return _userId;
// }

View File

@@ -2,14 +2,21 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:wger/providers/auth.dart';
import 'package:wger/providers/workout_plan.dart';
class WorkoutPlans with ChangeNotifier {
static const workoutPlansUrl = 'http://10.0.2.2:8000/api/v2/workout/';
List<WorkoutPlan> _entries = [];
final String authToken;
static const workoutPlansUrl = '/api/v2/workout/';
WorkoutPlans(this.authToken, this._entries);
String _url;
List<WorkoutPlan> _entries = [];
Auth _auth;
WorkoutPlans(Auth auth, List<WorkoutPlan> entries) {
this._auth = auth;
this._entries = entries;
this._url = auth.serverUrl + workoutPlansUrl;
}
List<WorkoutPlan> get items {
return [..._entries];
@@ -21,8 +28,8 @@ class WorkoutPlans with ChangeNotifier {
Future<void> fetchAndSetWorkouts() async {
final response = await http.get(
workoutPlansUrl,
headers: <String, String>{'Authorization': 'Token $authToken'},
_url,
headers: <String, String>{'Authorization': 'Token ${_auth.token}'},
);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
@@ -49,7 +56,7 @@ class WorkoutPlans with ChangeNotifier {
Future<void> addProduct(WorkoutPlan product) async {
final productsUrl =
'https://flutter-shop-a2335.firebaseio.com/products.json?auth=$authToken';
'https://flutter-shop-a2335.firebaseio.com/products.json?auth=${_auth.token}';
try {
final response = await http.post(
productsUrl,
@@ -76,7 +83,7 @@ class WorkoutPlans with ChangeNotifier {
final prodIndex = _entries.indexWhere((element) => element.id == id);
if (prodIndex >= 0) {
final url =
'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=$authToken';
'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
await http.patch(url,
body: json.encode({
'description': newProduct.description,
@@ -88,7 +95,7 @@ class WorkoutPlans with ChangeNotifier {
Future<void> deleteProduct(String id) async {
final url =
'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=$authToken';
'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}';
final existingProductIndex =
_entries.indexWhere((element) => element.id == id);
var existingProduct = _entries[existingProductIndex];