Create Random sequence of numbers from Sum of Google Sheet call.

Database & APIs

Hello Community!

This is my first post. I am very new to FlutterFlow. Coming from Godot.

I need to pull the sum of a Google Sheet List and then create a sequence of random numbers from that total. I have my Google sheet API connected. I just don't know where to go from there. I have done some research and tried some codes I found on the internet/AI but they don't work. I'll take any advice or help.

Thank you!!

What have you tried so far?

This is one of the many codes I have tried.

Future<double> calculateSum(
      String spreadsheetId, String range, String apiKey) async {
    if (apiKey == null || spreadsheetId == null || range == null) {
      return 0;
    }

    final url =
        'https://sheets.googleapis.com/v4/spreadsheets/$spreadsheetId/values/$range?key=$apiKey';

    final response = await http.get(Uri.parse(url));

    if (response.statusCode != 200) {
      return 0;
    }

    final json = jsonDecode(response.body);

    if (json['values'] == null) {
      return 0;
    }

    final values = json['values'] as List<dynamic>;

    if (values.isEmpty) {
      return 0;
    }

    final sum = values
        .map((row) => row
            .map((cell) => double.tryParse(cell) ?? 0)
            .reduce((a, b) => a + b))
        .reduce((a, b) => a + b);

    return sum;
  }

  void main() async {
    final spreadsheetId = 'your_spreadsheet_id_here';
    final range = 'Sheet1!A1:B10'; // Adjust the range as needed
    final apiKey = 'your_api_key_here';

    final result = await calculateSum(spreadsheetId, range, apiKey);
    if (result != null) {
      print('Sum of values: $result');
    } else {
      print('Error fetching data or empty values.');
    }
  }
Did you check FlutterFlow's Documentation for this topic?
Yes
2 replies