In dart language, How to initialize default value for Generic type variable?

1,998 views
Skip to first unread message

Ashok Kuvaraja

unread,
Mar 18, 2020, 8:59:34 AM3/18/20
to Flutter Development (flutter-dev)
 How to initialize default value for Generic type variable?   
   class RangeValuePair<T>
   {
      T _value ;
      RangeValuePair(int start)
      {
         this._value = ?(how to define) ;
      } 
   }

Suzuki Tomohiro

unread,
Mar 18, 2020, 9:49:26 AM3/18/20
to Ashok Kuvaraja, Flutter Development (flutter-dev)

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/e1d8f7d0-8143-44b6-855d-995bdecea5d9%40googlegroups.com.

Ashok Kuvaraja

unread,
Mar 19, 2020, 1:40:06 AM3/19/20
to Flutter Development (flutter-dev)
Hi, 
     Thank you for your response. I need to set the default value of generic type in runtime. In c#, default(T) function can use to set the default value of generic type in runtime. I need to perform this action in dart language. How to initialize?


On Wednesday, March 18, 2020 at 7:19:26 PM UTC+5:30, Suzuki Tomohiro wrote:
On Wed, Mar 18, 2020 at 8:59 AM Ashok Kuvaraja <kuvara...@gmail.com> wrote:
 How to initialize default value for Generic type variable?   
   class RangeValuePair<T>
   {
      T _value ;
      RangeValuePair(int start)
      {
         this._value = ?(how to define) ;
      } 
   }

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

tomohiro suzuki

unread,
Mar 19, 2020, 7:56:26 AM3/19/20
to Ashok Kuvaraja, Flutter Development (flutter-dev)
I don’t think of the solution. If you cannot tell the concrete type, you don’t know the initial value. Curious; what is your logic in C#?

Sent from my iPad

On Mar 19, 2020, at 1:40 AM, Ashok Kuvaraja <kuvara...@gmail.com> wrote:


To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/20ba30c3-f746-419b-9f3e-61db1ecb94dc%40googlegroups.com.

Terje A. Bergesen

unread,
Mar 19, 2020, 12:49:52 PM3/19/20
to tomohiro suzuki, Ashok Kuvaraja, Flutter Development (flutter-dev)
In C# all types have a default value. Objects and strings are null. Numbers tend to be zero etc. So, if you have a generic type in C#, you can always do

var t = default(T);

The run-time environment knows what the default value of whatever T is and it sets it accordingly.




--
Terje A Bergesen - te...@bergesen.info
"Vae, puto deus fio" -- Vespasian, not his last words though

Tom Gilmour

unread,
Mar 19, 2020, 2:05:09 PM3/19/20
to Terje A. Bergesen, tomohiro suzuki, Ashok Kuvaraja, Flutter Development (flutter-dev)
Maybe this is the Dart code you're looking for?

class RangeValuePair<T>{
T _value;
//Constructor
RangeValuePair(this._value);

// getter
get value {return this._value;}
> To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/CAHRobMt8TDZWeeMtN8FR6DL7kHNQTyFE8%3DbiN5uFJMKu0yBEdA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages