getAnnouncements method Null safety

Future<List<Announcement>?> getAnnouncements(
  1. {GeneralCallback<List<Announcement>?>? callback,
  2. String? locale,
  3. List<String>? tags,
  4. bool sortAndRandom = true}
)

Implementation

Future<List<Announcement>?> getAnnouncements({
  GeneralCallback<List<Announcement>?>? callback,
  String? locale,
  List<String>? tags,
  bool sortAndRandom = true,
}) async {
  try {
    final Response<Map<String, dynamic>> response = await dio.post(
      '/announcements',
      data: <String, dynamic>{
        'tag': tags ?? <String>[],
        'lang': locale ?? 'zh',
      },
    );
    AnnouncementData data = AnnouncementData(data: <Announcement>[]);
    if (response.statusCode != 204) {
      data = AnnouncementData.fromJson(response.data!);
      if (sortAndRandom) data.sortAndRandom();
    }
    if (callback != null) {
      callback.onSuccess(data.data);
    }
    return data.data;
  } on DioError catch (dioError) {
    if (callback == null) {
      rethrow;
    } else {
      callback.onFailure(dioError);
    }
  }
  return null;
}