Nikhil R
Nikhil R

Reputation: 115

How to fetch stream of reddit comments using flutter package draw library?

I'm trying to fetch a stream of Reddit comments from the subreddit in flutter app. Using Python is very simple using praw library API but when I try using dart wrapper draw I couldn't able to fetch data here is my code which I'm trying to implement

when I print comments I get I/flutter (30495): Instance of '_ControllerStream<Comment>'

using this Instance how can I fetch individual streams full of data?

I tried using for each method and fetch value

I ended up getting Error

    E/flutter (30495): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: DRAWNotFoundException(Reason: "null", Message: "Bad Request")
E/flutter (30495): #0      parseAndThrowError
package:draw/src/exception_objector.dart:15
E/flutter (30495): #1      Authenticator._request
package:draw/src/auth.dart:230
E/flutter (30495): <asynchronous suspension>
E/flutter (30495): #2      Reddit.get
package:draw/src/reddit.dart:633
E/flutter (30495): <asynchronous suspension>
E/flutter (30495): #3      ListingGenerator.generator._nextBatch
package:draw/…/listing/listing_generator.dart:70

in python I can just print comments by looping through this instance

for comment in reddit.subreddit('cricket').stream.comments(skip_existing=True):
          print(comment.body)

how can I achieve similar result in dart?

there's isn't much information in flutter draw package documentation here is the definition of this class draw docc but its not easily understandable to my requirement , can someone point me out in right direction to achieve this??

Upvotes: 0

Views: 363

Answers (1)

Anhar Alsaeed
Anhar Alsaeed

Reputation: 21

 import 'package:draw/draw.dart';

void fetchRedditComments() async {
  try {
    var reddit = await Reddit.createReadOnlyInstance(
      clientId: 'your-client-id', 
      clientSecret: 'your-client-secret', 
      userAgent: 'your-user-agent'
    );
    var subreddit = reddit.subreddit('cricket');
  
    // Dealing with comments using await for:
    await for (var comment in subreddit.stream.comments(skipExisting: true)) {
      print(comment.body);
    }
  } catch (e) {
    print('Error fetching comments: $e');
  }
}

Upvotes: 1

Related Questions