Flutter/Dart: How to replace asset file with API query result

i have the following code:

import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:syncfusion_flutter_datepicker/datepicker.dart';
// import 'package:http/http.dart' as http;

class CalendarApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Calendar Demo',
      theme: ThemeData(useMaterial3: false),
      home: const DateRangePicker(jsonDataRaw: 'assets/data.json'),
    );
  }
}

class DateRangePicker extends StatefulWidget {
  const DateRangePicker({
    super.key,
    this.width,
    this.height,
    required this.jsonDataRaw,
    this.simpleText,
    this.jsonListData,
  });

  final double? width;
  final double? height;
  final String jsonDataRaw;
  final String? simpleText;
  final dynamic jsonListData;

  // print(widget.jsonListData);

  @override
  State<DateRangePicker> createState() => _DateRangePickerState();
}

class _DateRangePickerState extends State<DateRangePicker> {
  List<DateTime> blackoutDates = <DateTime>[
    DateTime(2024, 03, 18),
    DateTime(2024, 03, 19)
  ];

  Future<String> getJsonFromAssets() async {
    return await rootBundle.loadString(widget.jsonDataRaw);
  }

  Future loadSalesData() async {
    final String jsonString = await getJsonFromAssets();
    final dynamic jsonResponse = json.decode(jsonString);
    blackoutDates.clear();
    for (Map<String, dynamic> i in jsonResponse['data']) {
      blackoutDates.add(DateTime.fromMillisecondsSinceEpoch(i['timestamp']));
    }
 
  }

  @override
  void initState() {
    loadSalesData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: FutureBuilder<String>(
        future: getJsonFromAssets(),
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          if (snapshot.hasData) {
            return SfDateRangePicker(
              monthViewSettings: DateRangePickerMonthViewSettings(
                firstDayOfWeek: 1,
                blackoutDates: blackoutDates,
              ),
              monthCellStyle: const DateRangePickerMonthCellStyle(
                blackoutDateTextStyle: TextStyle(
                  color: Colors.red,
                  decoration: TextDecoration.lineThrough,
                ),
              ),
              selectionMode: DateRangePickerSelectionMode.range,
              enablePastDates: false,
            );
          } else {
            return const CircularProgressIndicator();
          }
        },
      ),
    );
  }
}

How can I replace the information from the file "assets/data.json" With the following request

curl --request POST \
  --url https://XXXXXXX-api.vercel.app/api/v2/reservations-forward-period \
  --header 'Content-Type: application/json' \
  --data '{
  "property_id": "eAKlmfMrACA6EWqnPWYK"
}'

I've tried several attempts, but I'm having a problem with data type validation

1 reply