1

I have a custom function in my FlutterFlow app with this code:

import 'dart:convert';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/custom_functions.dart';
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/backend/schema/structs/index.dart';
import 'package:http/http.dart' as http;

Future<List<double>?> getLatLngFronAddress(String address)  async{
  /// MODIFY CODE ONLY BELOW THIS LINE

  final apiKey =
      'YOUR_API_KEY'; // Inserisci qui la tua API Key oppure usa un secret param
  final url = Uri.parse(
      'https://maps.googleapis.com/maps/api/geocode/json?address=${Uri.encodeComponent(address)}&key=$apiKey');

  final response = await http.get(url);

  if (response.statusCode == 200) {
    final data = json.decode(response.body);

    if (data['status'] == 'OK') {
      final location = data['results'][0]['geometry']['location'];
      final lat = location['lat'];
      final lng = location['lng'];
      return [lat, lng];
    } else {
      //debugPrint('Geocoding failed: ${data['status']}');
    }
  } else {
    //debugPrint('HTTP error: ${response.statusCode}');
  }
  return null;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

flutterflow release an error:

but flutter flow doesn't save the function whith the messagge:

The function is empty or cannot be parsed. Make sure not to modify code outside of the designated area.

I need import the http class and make the function async/future.

2

1 Answer 1

1

Custom Functions in FlutterFlow cannot be async and cannot perform custom imports. The template code tries to make that clear by including these markings:

/// MODIFY CODE ONLY BELOW THIS LINE
...
/// MODIFY CODE ONLY ABOVE THIS LINE

Ignoring these instructions leads to the type of error you're getting.

Following the instructions means that Custom Functions are limited to performing synchronous operations on their inputs with the functionality that FlutterFlow already imports.

To perform the type of action you want to do, you'll typically use a Custom Action, which can be asynchronous (in fact: most actions are async) and perform custom imports.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.