Apply a function to dynamic children from an API call

Actions & Logic

I have an API call that returns a list of devices, something like this:

{
  "data": {
    "property": {
      "devices": {
        "nodes": [
          {
            "id": "675c672e-cbb5-11ec-9294-d8c0a6cf76d9",
            "name": "don_eb2_125",
            "lastOnline": "2022-09-22T15:51:57.960000+00:00"
          },
          {
            "id": "89515d68-433d-11ec-8a8b-841b7765683c",
            "name": "don_p3_19",
            "lastOnline": null
          }
        ]
      }
    }
  }
}


I'm trying to generate dynamic children with this information, but I've run into multiple problems along the way.

  1. My lastOnline field isn't automatically converted to a date (as noted in this issue)

  2. I'd like to use the timeago library but I need to wrap my function in a custom action so that I can use a custom import

What have you tried so far?

I've created the custom action like so:

// Automatic FlutterFlow imports
import '/backend/schema/structs/index.dart';
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:timeago/timeago.dart' as timeago;

String convertTimestampToFuzzyTime(
  DateTime? timestampDateTime,
  String? timestampString,
) {
  var timestamp = timestampDateTime;
  if (timestamp == null) {
    if (timestampString == null) {
      return '';
    }
    timestamp = DateTime.parse(timestampString);
  }

  return timeago.format(timestamp);
}

So I'm almost there, but I can't apply an action to each child of a dynamic list. I could do this with a function on rendering the text, but I can't import timeago as part of a function ๐Ÿ™

Is there a way to do this? Do I need to do something like an API Interceptor? That seems a bit heavy-handed!

Did you check FlutterFlow's Documentation for this topic?
Yes
1
3 replies