update method

Future<List<Map<String, dynamic>>> update(
  1. dynamic flairList, {
  2. String text = '',
  3. String cssClass = '',
})

Set or clear the flair of multiple Redditors at once.

flairList can be one of: * List<String> of Redditor names * List<RedditorRef> * List<Flair>

text is the flair text to use when flair_text is missing or flairList is not a list of mappings.

cssClass is the CSS class to use when flair_css_class is missing or flairList is not a list of mappings.

Returns Future<List<Map<String, String>>> containing the success or failure of each update.

Implementation

Future<List<Map<String, dynamic>>> update(
    /* List<String>,
       List<RedditorRef>,
       List<Flair> */
    flairList,
    {String text = '',
    String cssClass = ''}) async {
  if ((flairList is! List<String>) &&
          (flairList is! List<RedditorRef>) &&
          (flairList is! List<Flair>) ||
      (flairList == null)) {
    throw DRAWArgumentError('flairList must be one of List<String>,'
        ' List<Redditor>, or List<Map<String,String>>.');
  }
  var lines = <String>[];
  for (final f in flairList) {
    if (f is String) {
      lines.add('"$f","$text","$cssClass"');
    } else if (f is RedditorRef) {
      lines.add('"${f.displayName}","$text","$cssClass"');
    } else if (f is Flair) {
      final name = f.user.displayName;
      final tmpText = f.flairText ?? text;
      final tmpCssClass = f.flairCssClass ?? cssClass;
      lines.add('"$name","$tmpText","$tmpCssClass"');
    } else {
      throw DRAWInternalError('Invalid flair format');
    }
  }
  final response = <Map<String, dynamic>>[];
  final url = apiPath['flaircsv']
      .replaceAll(_kSubredditRegExp, _subreddit.displayName);
  while (lines.isNotEmpty) {
    final batch = lines.sublist(0, min(100, lines.length));
    final data = <String, String>{
      'flair_csv': batch.join('\n'),
    };
    final List<Map<String, dynamic>> maps =
        (await _subreddit.reddit.post(url, data))
            .cast<Map<String, dynamic>>();
    response.addAll(maps);
    if (lines.length < 100) {
      break;
    }
    lines = lines.sublist(min(lines.length - 1, 100));
  }
  return response;
}