skip to Main Content

I passed arguments to next page, and I’m trying to fetch other data using arguments in flutter.

I’m using supabase, the data I’m trying to fetch is thumbNailUrl.

Navigator.of(context).pushNamed(
  '/postScreen',
  arguments: postId,
);
post_id(int) thumbNailUrl(String)
~~~ ~~~

But I cannot understand how to fetch thumbNailUrl using arguments, and I want to enter parameters into postImage().

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SingleChildScrollView(
      child: Column(
        children: [
          postImage( ~ ),
          .
          .
          .
        ],
      ),
    ),
  );
}

Please help me,,

2

Answers


  1. Chosen as BEST ANSWER

    I barely solve it,, hope it helps others to use supabase and dart.

    // previous_screen.dart
    
    Navigator.of(context).pushNamed(
      '/postScreen',
      arguments: postId,
    );
    
    // post_screen.dart
    
    Future fetchData(int postId) async {
      final data = await Supabase.instance.client
        .from('board')
        .select('thumbnail_url, title')
        .eq('post_id', postId)
        .single();
    
      final result = [
        data['thumbnail_url'],
        data['title'],
      ];
      return result;
    }
    
    @override
    Widget build(BuildContext context) {
      final postId = ModalRoute.of(context)!.settings.arguments as int; // use this type carefully.
      return FutureBuilder(
        future: fetchData(postId),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          final thumbNailUrl = snapshot.data[0];
          final title = snapshot.data[1];
          return Scaffold(
            body: Column(
              YourWidget(thumbNailUrl, title),
            ),
          );
        },
      );
    }
    

  2.  Navigator.of(context).pushNamed(
          '/postScreen',
    
          ///
          /// Arugments type is a Object? so you can pass any type of data
          /// - String
          /// - int
          /// - double
          /// - bool
          /// - List
          /// - Map
          /// - Class
          /// - Enum
          /// - etc...
          ///
          arguments: AnyClass(1, ''),
        );
    

    PostScreen

        ///
        /// Here /PostScreen
        ///
        final args = ModalRoute.of(context)!.settings.arguments;
    
        if (args is AnyClass) {
          print(args.postId);
          print(args.postImageUrl);
        }
    

    AnyClass

    This is an example you should create a new one for yourself

    final class AnyClass {
      AnyClass(this.postId, this.postImageUrl);
      final int postId;
      final String postImageUrl;
    }
    

    Source: https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search