The argument type 'String' can't be assigned to the parameter type 'AllPosts'

Viewed 27

I'm not sure why it can't saveform. Please help me QAQ this is messing up my life

This is the error: lib/navpages/upload/edit_post_screen.dart:98:65: Error: The argument type 'String' can't be assigned to the parameter type 'AllPosts'.

  • 'AllPosts' is from 'package:pinsta/providers/all_posts.dart' ('lib/providers/all_posts.dart'). onPressed:() => saveForm(i,caption!), ^

     import 'package:flutter/material.dart';
     import 'package:pinsta/models/post.dart';
     import 'package:pinsta/providers/all_posts.dart';
     import 'package:provider/provider.dart';
    
    
     class EditPostScreen extends StatefulWidget {
       static String routeName = '/edit-post';
    
       @override
       State<EditPostScreen> createState() => _EditPostScreenState();
     }
    
     class _EditPostScreenState extends State<EditPostScreen> {
       var form = GlobalKey<FormState>();
    
       String? caption?? ;
    
    
    
       void saveForm(int i, AllPosts myPosts) {
         bool isValid = form.currentState!.validate();
         if (isValid) {
           form.currentState!.save();
           print(caption);
    
           myPosts.updatePost(i,caption);
    
           FocusScope.of(context).unfocus();
           form.currentState!.reset();
           caption = null;
    
           ScaffoldMessenger.of(context).showSnackBar(
             SnackBar(
               content: Row(
                 children: [
                   Flexible(child: Text('updated', style: TextStyle(color: Colors.white, fontSize: 14.5))),
                 ],
               ),
               duration: Duration(seconds: 1),
             ),
           );
    
         }
       }
    
    
       @override
       Widget build(BuildContext context) {
         AllPosts postList1 = Provider.of <AllPosts> (context);
         int i = ModalRoute.of(context)?.settings.arguments as int;
    
         return Scaffold(
           resizeToAvoidBottomInset: false,
           body: SafeArea(
             child: Padding(
               padding: EdgeInsets.symmetric(horizontal: 12.0,vertical: 18.0),
               child: Column(
                 crossAxisAlignment: CrossAxisAlignment.center,
                 children: [
                   Image.asset('assets/images/upload_image.png',height: 200,
                     width: 200,),
                   SizedBox(height: 50.0,),
                   Form(
                     key: form,
                     child: Column(
                       children: [
                         SizedBox(
                           height: 60.0,
                         ),
                         TextFormField(
                           decoration: InputDecoration(
                               prefixIcon: Icon(Icons.people, color: Colors.grey),
                               hintText: 'caption',
                               border: OutlineInputBorder(
                                   borderRadius: BorderRadius.circular(24.0)
                               )
                           ),
                           validator: (value) {
                             if (value == null)
                               return caption;
                           },
                           onSaved: (value) {caption = value;},
                         ),
                         SizedBox(
                           height: 50.0,
                         ),
                         Padding(
                           padding: const EdgeInsets.symmetric(horizontal: 25.0),
                           child: Container(
                               padding: EdgeInsets.all(5),
                               decoration: BoxDecoration(
                                 color: Colors.redAccent,
                                 borderRadius: BorderRadius.circular(12),
                               ),
                               child: Center(
                                 child: TextButton(
                                   onPressed:() => saveForm(i,caption!),
                                   child: Text(
                                     'Update', style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold, color: Colors.white),
                                   ),
                                 ),
                               )
                           ),
                         ),
                       ],
                     ),
                   ),
                 ],
               ),
             ),
           ),
         );
       }
     }
    

This is the provider:

    import 'package:flutter/material.dart';
    import '../models/post.dart';
    
    class AllPosts with ChangeNotifier {
      List <Post> myPosts = [];
    
      List<Post> getMyPosts() {
        return myPosts;
      }
    
      void addPost(imageUrl, caption, username) {
        myPosts.insert(0, Post(imageUrl: imageUrl, caption: caption, username:username));
        notifyListeners();
      }
    
      void removePost(i) {
        myPosts.removeAt(i);
        notifyListeners();
      }
    
      void updatePost(i,caption ) {
        myPosts[i].caption = caption;
        notifyListeners();
      }
    }
1 Answers

You method void saveForm(int i, AllPosts myPosts) has AllPosts as the 2nd parameter but you are calling it with saveForm(i,caption!), passing a String (caption) as argument.

Related