How do I extend a List in Dart?

后端 未结 5 1230
醉梦人生
醉梦人生 2020-11-29 07:14

I want to create a more specialized list in dart. I can\'t directly extend List. What are my options?

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 08:02

    The answers to this are pretty outdated, and I'm in the process of doing this for my own project, so I thought I'd help some people out by posting a really clean answer that doesn't involve any overriding or implementing of things.

    The quiver package has an extendable List class called DelegatingList that makes extending a list trivial.

    class FruitList extends DelegatingList {
        final List _fruits = [];
    
        List get delegate => _fruits;
    
        // custom methods
    }
    

    Hopefully this helps someone who comes across this question like I did!

提交回复
热议问题